https://github.com/wasth/wejr
Websocket with JSONRPC for Django Channels
https://github.com/wasth/wejr
Last synced: about 1 year ago
JSON representation
Websocket with JSONRPC for Django Channels
- Host: GitHub
- URL: https://github.com/wasth/wejr
- Owner: Wasth
- Created: 2023-01-23T15:10:35.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-01-30T12:53:15.000Z (over 3 years ago)
- Last Synced: 2025-01-18T03:41:04.034Z (over 1 year ago)
- Language: Python
- Size: 17.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# [WIP] Websocket by JSON-RPC protocol for Django Channels
Library for using websocket in django(with channels) in jsonrpc way
### JSON-RPC specification
https://www.jsonrpc.org/specification
## Installing
WIP
## Usage example
Add wejr to django INSTALLED_APPS before your apps
```python
INSTALLED_APPS = [
...
"wejr",
...
]
```
Configure channel layer via Redis PubSub
```python
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.pubsub.RedisPubSubChannelLayer",
"CONFIG": {
"hosts": [("localhost", 6379)],
},
},
}
```
Define routers, methods, params schemas
```python
from dataclasses import dataclass, asdict
from wejr.router import Router
@dataclass
class ChatId:
id: int
@dataclass
class MessageCreated:
author_id: int
content: str
chat_id: int
client = Router()
chat = Router()
@client.method
async def chat_by_id(consumer, request, params: ChatId):
chat = get_chat_data(params.id)
consumer.send_result(request, chat)
@chat.method
async def message_created(consumer, request, params: MessageCreated):
consumer.send_notification("", asdict(params))
```
Define consumer
```python
from wejr.consumers import AsyncBaseJsonRpcConsumer
from myproject.app.routers import client, chat
class MyConsumer(AsyncBaseJsonRpcConsumer):
client_router = client
group_routers = {
"chat": chat,
}
```
Define urls
```python
urlpatterns = [
re_path(r"v1/ws", MyConsumer.as_asgi()),
]
```
Extend asgi with websocket
```python
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application
from djangochatws.apps.threadws import urls as ws_urls
application = ProtocolTypeRouter(
{
"http": get_asgi_application(),
"websocket": AuthMiddlewareStack(
URLRouter(ws_urls.urlpatterns)
)
}
)
```