taoufik07 / nejma

Manage and send messages to groups of channels
BSD 3-Clause "New" or "Revised" License
46 stars 7 forks source link

Type handlers #2

Open taoufik07 opened 5 years ago

taoufik07 commented 5 years ago

Hey,

Channels allows code decoupling by dispatching messages to handlers chat.join triggers chat_join.

I've made something similar while playing with asyncio and decided to try to implement it in nejma (checkout this branch).

Usage (a gist with the whole app)

@app.websocket_route("/ws")
class Chat(WebSocketEndpoint):
    encoding = "json"

    async def on_receive(self, websocket, data):
        ...
        group = f"group_{room_id}"

        self.channel_layer.add(group, self.channel)

        await self.channel_layer.group_send(group, {
            "type": "hello.world",
            "message": "Hello world !",
        })

    async def hello_world(self, payload):
        await self.send(payload)

I wanna know what you guys and if it should be in master ?

taoufik07 commented 5 years ago

it's integrated with starlette for now, it's just a test and the implementation need to be reviewed to have loose coupling and reusable components. Also feedback about the implementation will be appreciated.

taoufik07 commented 5 years ago

@andrewgodwin @erm @kcrebound @tomchristie if you got some free time, your feedback will be greatly appreciated :D