Channels Easy
A thin wrapper around channel consumers to make things EASY
Note: This library currently support only text data which is JSON serializable.
What problem does this library solve?
This library simplifies two tasks for now
- Parse incoming text data as JSON and vice versa.
- Generate event on the basis of type passed from client side.
Table of Contents
Installation
To get the latest stable release from PyPi
pip install channels-easy
As channels-easy is a thin wrapper around channels so channels must be in your INSTALLED_APPS in settings.py.
INSTALLED_APPS = (
...,
'channels',
)
Example
All the naming convention used to implement this library is inspired from socket.io to make server implementation simple.
Get full example project here.
Server side
# consumers.py
from channels_easy.generic import AsyncWebsocketConsumer
class NewConsumer(AsyncWebsocketConsumer):
async def connect(self):
# join room on connect
await self.join("room1")
await self.accept()
async def disconnect(self, close_code):
# Leave room on disconnect
await self.leave("room1")
async def on_message(self, data):
print("message from client", data)
# output:
# message from client {'text': 'hello'}
await self.emit("message", {"message": "hello from server"}, "room1")
Client side
// client.js
const socket = new WebSocket("ws://localhost:8000/ws/test/");
socket.onmessage = function ({ data }) {
const parsed_data = JSON.parse(data);
console.log(parsed_data);
// output:
// {
// data: {message: 'hello from server'}
// type: "message"
// }
};
socket.onopen = () => {
console.log("websocket connected...");
// send message from client after connected
// send with type `message` to receive from subscribed
// `on_message` event on server side
socket.send(
JSON.stringify({
type: "message",
data: {
text: "hello",
},
})
);
};
API Usage
Subscribing to events We can simply subscribe to a message type as
def on_<type>(self, data):
...
pass
so if client send data as
{
"type": "message",
"data": "Hello!"
}
We can subscribe to message event as
def on_message(self, data):
...
pass
Emitting Message
We can emit message to client using same schema that we used above
def on_message(self, data):
...
# some code here
...
self.emit(
"message", # type
{"text": "hello"}, # message dict | str | int | list
["room1"], # room list or string
)
Check all APIs here.
Contribute
If you want to contribute to this project, please perform the following steps
# Fork this repository
# Clone your fork
poetry install
git checkout -b feature_branch master
# Implement your feature and tests
git add . && git commit
git push -u origin feature_branch
# Send us a pull request for your feature branch
In order to run the tests, simply execute poetry run pytest. This will run test created inside
test directory.
Metadata
Release files for channels-easy 0.3.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 | |
|---|---|---|---|
| channels-easy-0.3.0.tar.gz | 5.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| channels_easy-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.9 kB
Release files / channels-easy-0.3.0.tar.gz
| Download URL | channels-easy-0.3.0.tar.gz |
|---|---|
| Size | 5.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
531ac814fec47ec8c4d9f58da204c7247129ed4bfdc415a006125a3516377c1c
|
|
BLAKE2b-256 checksum How to use checksums |
3bf67b5b34f147479755adde5b53fed4dacfae591292ee836e60a74d4700bf0f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.12 CPython/3.9.9 Linux/5.11.0-1025-azure
|
Release files / channels_easy-0.3.0-py3-none-any.whl
| Download URL | channels_easy-0.3.0-py3-none-any.whl |
|---|---|
| Size | 5.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
37614cf6847032bece2c78ad786a2fac7edbf4a31fab3a0eadca99f169bbc419
|
|
BLAKE2b-256 checksum How to use checksums |
968e3bcb08b6c472613b24b04da998cd55c3a27210ab165436b267c6e2eba5d9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.12 CPython/3.9.9 Linux/5.11.0-1025-azure
|