|
| 1 | +# RSocket with Django Channels |
| 2 | + |
| 3 | +This example demonstrates how to use RSocket with Django Channels, allowing you to implement RSocket protocol support in your Django applications. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +Django Channels extends Django to handle WebSockets, and this integration allows you to use the RSocket protocol over those WebSocket connections. This enables: |
| 8 | + |
| 9 | +- Reactive streaming capabilities in Django applications |
| 10 | +- Support for all RSocket interaction models (request-response, fire-and-forget, request-stream, request-channel) |
| 11 | +- Bidirectional communication between client and server |
| 12 | + |
| 13 | +## Requirements |
| 14 | + |
| 15 | +- Django 3.0+ |
| 16 | +- Channels 4.0+ |
| 17 | +- An ASGI server like Daphne or Uvicorn |
| 18 | + |
| 19 | +## Installation |
| 20 | + |
| 21 | +1. Install Django Channels: |
| 22 | + ```bash |
| 23 | + pip install channels |
| 24 | + ``` |
| 25 | + |
| 26 | +2. Install an ASGI server: |
| 27 | + ```bash |
| 28 | + pip install daphne |
| 29 | + ``` |
| 30 | + |
| 31 | +3. Configure your Django project to use Channels (see Django Channels documentation) |
| 32 | + |
| 33 | +## Server Setup |
| 34 | + |
| 35 | +1. Create a request handler for RSocket: |
| 36 | + ```python |
| 37 | + from rsocket.payload import Payload |
| 38 | + from rsocket.request_handler import BaseRequestHandler |
| 39 | + |
| 40 | + class Handler(BaseRequestHandler): |
| 41 | + async def request_response(self, payload: Payload): |
| 42 | + return Payload(b'Echo: ' + payload.data) |
| 43 | + ``` |
| 44 | + |
| 45 | +2. Create an RSocket consumer using the factory: |
| 46 | + ```python |
| 47 | + from rsocket.transports.channels_transport import rsocket_consumer_factory |
| 48 | + |
| 49 | + RSocketConsumer = rsocket_consumer_factory(handler_factory=Handler) |
| 50 | + ``` |
| 51 | + |
| 52 | +3. Add the consumer to your routing configuration: |
| 53 | + ```python |
| 54 | + from django.urls import path |
| 55 | + from channels.routing import ProtocolTypeRouter, URLRouter |
| 56 | + |
| 57 | + application = ProtocolTypeRouter({ |
| 58 | + 'websocket': URLRouter([ |
| 59 | + path('rsocket', RSocketConsumer.as_asgi()), |
| 60 | + ]), |
| 61 | + }) |
| 62 | + ``` |
| 63 | + |
| 64 | +## Client Usage |
| 65 | + |
| 66 | +You can connect to your Django Channels RSocket server using any RSocket client. Here's an example using the Python client: |
| 67 | + |
| 68 | +```python |
| 69 | +import asyncio |
| 70 | +import websockets |
| 71 | +from rsocket.helpers import single_transport_provider |
| 72 | +from rsocket.payload import Payload |
| 73 | +from rsocket.rsocket_client import RSocketClient |
| 74 | +from rsocket.transports.websockets_transport import WebsocketsTransport |
| 75 | + |
| 76 | +async def main(): |
| 77 | + async with websockets.connect('ws://localhost:8000/rsocket') as websocket: |
| 78 | + transport = WebsocketsTransport() |
| 79 | + handler_task = asyncio.create_task(transport.handler(websocket)) |
| 80 | + |
| 81 | + try: |
| 82 | + async with RSocketClient(single_transport_provider(transport)) as client: |
| 83 | + response = await client.request_response(Payload(b'Hello')) |
| 84 | + print(f"Received: {response.data.decode()}") |
| 85 | + finally: |
| 86 | + handler_task.cancel() |
| 87 | + try: |
| 88 | + await handler_task |
| 89 | + except asyncio.CancelledError: |
| 90 | + pass |
| 91 | + |
| 92 | +if __name__ == '__main__': |
| 93 | + asyncio.run(main()) |
| 94 | +``` |
| 95 | + |
| 96 | +## Advanced Usage |
| 97 | + |
| 98 | +The Django Channels transport supports all RSocket interaction models: |
| 99 | + |
| 100 | +- **Request-Response**: Simple request with a single response |
| 101 | +- **Fire-and-Forget**: One-way message with no response |
| 102 | +- **Request-Stream**: Request that receives a stream of responses |
| 103 | +- **Request-Channel**: Bi-directional stream of messages |
| 104 | + |
| 105 | +See the server_example.py and client_example.py files for more detailed examples. |
| 106 | + |
| 107 | +## Security Considerations |
| 108 | + |
| 109 | +- Use secure WebSockets (wss://) in production |
| 110 | +- Implement proper authentication and authorization in your Django application |
| 111 | +- Consider using RSocket's authentication and authorization extensions for additional security |
0 commit comments