⭐ Nejma ⭐
Nejma ⭐ allows you to manage multiple realtime connections and send messages to groups or a user multiple tabs...
Take a look at this example nejma-chat, a simple chat application built using nejma and starlette.
Installation
$ pip install nejma
Getting started
Here's an example of using nejma with websockets.
First import Channel and channel_layer from nejma
from nejma import Channel, channel_layer
Create a channel on connect
async def on_connect(self, websocket, **kwargs):
await super().on_connect(websocket, **kwargs)
self.channel = Channel(send=websocket.send)
Add groups, channels or send messages
async def on_receive(self, websocket, data):
# Adds a channel to a giving group
self.channel_layer.add(group, self.channel)
# Removes a channel from a given group
self.channel_layer.remove(group, self.channel)
# Removes a channel from all the groups
self.channel_layer.remove_channel(self.channel)
# Reset all the groups
self.channel_layer.flush()
await self.channel_layer.group_send(group, "Welcome !")
Finnaly, remove the channel once the connection is closed
async def on_disconnect(self, websocket, close_code):
self.channel_layer.remove_channel(self.channel)
Starlette
To use nejma with starlette, simply import the WebSocketEndpoint from nejma
from channels.ext.starlette import WebSocketEndpoint
@app.websocket_route("/ws")
class Chat(WebSocketEndpoint):
encoding = "json"
async def on_receive(self, websocket, data):
room_id = data['room_id']
message = data['message']
username = data['username']
if message.strip():
group = f"group_{room_id}"
self.channel_layer.add(group, self.channel)
payload = {
"username": username,
"message": message,
"room_id": room_id
}
await self.channel_layer.group_send(group, payload)
Docs
The ChannelLayer class provided by nejma exposes the following methods :
add(group, channel)
Adds a channel to a giving group.
self.channel_layer.add(group, self.channel)
remove(group, channel)
Removes a channel from a given group
self.channel_layer.remove(group, self.channel)
remove_channel(channel)
Removes a channel from all the groups
self.channel_layer.remove_channel(self.channel)
flush()
Reset all the groups
self.channel_layer.flush()
Metadata
Release files for nejma 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| nejma-0.1.0.tar.gz | 3.3 kB | Without provenance | Details for nejma-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| nejma-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for nejma-0.1.0-py3-none-any.whl |
Total release size: 8.4 kB
Release files / nejma-0.1.0.tar.gz
| Download URL | nejma-0.1.0.tar.gz |
|---|---|
| Size | 3.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
21700901af1eac11ff04854c3cefb8ef1f7d6abd831c89347a1a326bb1deac9b
|
|
BLAKE2b-256 checksum How to use checksums |
bf85c0a72b6c944b6349968575dd0fdcd602047ca105f171872156a4068febae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.4.2 requests/2.18.4 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.7
|
Release files / nejma-0.1.0-py3-none-any.whl
| Download URL | nejma-0.1.0-py3-none-any.whl |
|---|---|
| Size | 5.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4553ad7fd79282325ba5f0ccc8ac980809dc32bbea420a7c1a37ef0c34161c9e
|
|
BLAKE2b-256 checksum How to use checksums |
4ca5044bdc4d0fe562ef76c915c4d745b8913a88348847702c86a8a43bbeabe0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.4.2 requests/2.18.4 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.7
|
Security for nejma 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.