{"id":13690350,"url":"https://github.com/taoufik07/nejma","last_synced_at":"2025-08-04T03:36:35.012Z","repository":{"id":57445417,"uuid":"170115179","full_name":"taoufik07/nejma","owner":"taoufik07","description":"Manage and send messages to groups of channels","archived":false,"fork":false,"pushed_at":"2019-10-27T14:24:52.000Z","size":9,"stargazers_count":47,"open_issues_count":2,"forks_count":7,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-03-26T07:22:37.543Z","etag":null,"topics":["asgi","channels","chat","fastapi","quart","responder","sanic","starlette"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/taoufik07.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-02-11T11:09:04.000Z","updated_at":"2025-03-06T04:29:48.000Z","dependencies_parsed_at":"2022-09-26T16:30:59.845Z","dependency_job_id":null,"html_url":"https://github.com/taoufik07/nejma","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taoufik07%2Fnejma","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taoufik07%2Fnejma/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taoufik07%2Fnejma/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taoufik07%2Fnejma/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/taoufik07","download_url":"https://codeload.github.com/taoufik07/nejma/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248144948,"owners_count":21055013,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asgi","channels","chat","fastapi","quart","responder","sanic","starlette"],"created_at":"2024-08-02T16:01:03.810Z","updated_at":"2025-04-12T12:44:03.298Z","avatar_url":"https://github.com/taoufik07.png","language":"Python","funding_links":[],"categories":["Extensions"],"sub_categories":["WebSocket","WebSockets"],"readme":"# ⭐ Nejma ⭐\n\n\nInspired by `channels`, Nejma ⭐ allows you manage and send messages to groups of channels\n\nThings you can build with `nejma`:\n- chat\n- notification\n- handle users connected from multiple tabs)\n- and more...\n\n\n## Example\n\nTake a look at this example [`nejma-chat`](https://github.com/taoufik07/nejma-chat), a simple chat application built using `nejma` and `starlette`.\n\n\n## Installation\n\n```shell\n$ pip install nejma\n```\n\n## Getting started \n\nHere's an example of using `nejma` with websockets.\n\nFirst import Channel and channel_layer from nejma\n\n```python\nfrom nejma import Channel, channel_layer\n```\n\nCreate a channel on connect\n\n```python\nasync def on_connect(self, websocket, **kwargs):\n    await super().on_connect(websocket, **kwargs)\n\n    self.channel = Channel(send=websocket.send)\n\n```\n\nAdd groups, channels or send messages   \n\n```python\nasync def on_receive(self, websocket, data):\n    self.channel_layer.add(group, self.channel)\n\n    await self.channel_layer.group_send(group, \"Welcome !\")\n```\n\nFinnaly, remove the channel once the connection is closed \n\n```python\nasync def on_disconnect(self, websocket, close_code):\n    self.channel_layer.remove_channel(self.channel)\n```\n\n\n### Starlette\n---\n\nTo use `nejma` with `starlette`, simply import the WebSocketEndpoint from nejma\n\n```python\nfrom nejma.ext.starlette import WebSocketEndpoint\n\n@app.websocket_route(\"/ws\")\nclass Chat(WebSocketEndpoint):\n    encoding = \"json\"\n\n    async def on_receive(self, websocket, data):\n        room_id = data['room_id']\n        message = data['message']\n        username = data['username']\n\n        if message.strip():\n            group = f\"group_{room_id}\"\n\n            self.channel_layer.add(group, self.channel)\n\n            payload = {\n                \"username\": username,\n                \"message\": message,\n                \"room_id\": room_id\n            }\n            await self.channel_layer.group_send(group, payload)\n```\n\n## Docs\n\nThe `ChannelLayer` class provided by `nejma` exposes the following methods :\n\n`add(group, channel, send=None)`\n\nAdds a channel to a giving group.\n\n* send : method to send messages to a channel\n\n```python\nself.channel_layer.add(group, self.channel, send=websocket.send)\n```\n\n`async group_send(group, \"Welcome !\")`\n\nSends a message to a group of channels\n\n```python\nawait self.channel_layer.group_send(group, \"Welcome !\")\n```\n\n`remove(group, channel)`\n\nRemoves a channel from a given group\n\n```python\nself.channel_layer.remove(group, self.channel)\n```\n\n`remove_channel(channel)`\n\nRemoves a channel from all the groups\n\n```python\nself.channel_layer.remove_channel(self.channel)\n```\n\n`flush()`\n\nReset all the groups\n\n```python\nself.channel_layer.flush()\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftaoufik07%2Fnejma","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftaoufik07%2Fnejma","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftaoufik07%2Fnejma/lists"}