Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redis channel layer #1

Open
chodex-xyz opened this issue Feb 28, 2019 · 2 comments
Open

Redis channel layer #1

chodex-xyz opened this issue Feb 28, 2019 · 2 comments
Labels
feature help wanted Extra attention is needed

Comments

@chodex-xyz
Copy link

Thanks for the package, It will be great to see Redis as a backing store

Attached some useful links
encode/starlette#133
https://github.com/bergran/websockets_starlette_example

@taoufik07
Copy link
Owner

Thanks :D
I'm gonna try to find some time for it, if someone else want to do it that would be great.

@taoufik07 taoufik07 added the help wanted Extra attention is needed label Apr 3, 2019
@gbozee
Copy link

gbozee commented Jul 18, 2019

Based on @kcrebound Implementation. I was able to come up with something.
The RedisLayer class

import asyncio
import json
import logging

import aioredis
from aioredis import Channel
from aioredis.pubsub import Receiver
from starlette.endpoints import WebSocketEndpoint as BaseWebSocketEndpoint, status
from starlette.websockets import WebSocketState, WebSocket

logger = logging.getLogger("websockets")

class RedisLayer(ChannelLayer):
    def __init__(self, redis_host, default_redis_channel_path=None):
        self.redis_host = redis_host
        self.initialized = False
        self.default_path = default_redis_channel_path or "default_path"

    async def _initialize(self):
        self.pub = await aioredis.create_redis(self.redis_host)
        self.sub = await aioredis.create_redis(self.redis_host)
        self.mpsc = Receiver(loop=asyncio.get_event_loop())
        self.initialized = True

    async def publish_to_redis(self, msg, path=None):
        if not self.initialized:
            await self._initialize()
        _path = path or self.default_path
        await self.pub.execute("publish", _path, json.dumps(msg))

    async def subscribe_to_redis(self, websocket, path=None, receive_callback=None):
        _path = path or self.default_path
        channel = Channel(_path, is_pattern=False)
        if not self.initialized:
            await self._initialize()
        try:
            result = await self.sub.subscribe(self.mpsc.channel(_path))
            async for channel, msg in self.mpsc.iter():
                if websocket.client_state == WebSocketState.CONNECTED:
                    msg = json.loads(msg)
                    if receive_callback:
                        await receive_callback(websocket, msg)
                    else:
                        await websocket.send_json(msg)
        except:
            import traceback

            traceback.print_exc()
            await self.close_connections(_path)
        finally:
            logger.info("Connection closed")

    async def close_connections(self, channel, *channels):
        await self.sub.unsubscribe(channel, *channels)
        self.mpsc.stop()
        self.pub.close()
        self.sub.close()

A Subclass of the default WebsocketEndpoint provided by starlette

class RedisWebSocketEndPoint(BaseWebSocketEndpoint):
    encoding = "json"

    async def dispatch(self) -> None:
        websocket = WebSocket(self.scope, receive=self.receive, send=self.send)
        await self.on_connect(websocket)
        await asyncio.gather(self._dispatch(websocket), self._redis_setup(websocket))

    async def _dispatch(self, websocket) -> None:
        close_code = status.WS_1000_NORMAL_CLOSURE

        try:
            while True:
                message = await websocket.receive()
                if message["type"] == "websocket.receive":
                    data = await self.decode(websocket, message)
                    await self.on_receive(websocket, data)
                elif message["type"] == "websocket.disconnect":
                    close_code = int(message.get("code", status.WS_1000_NORMAL_CLOSURE))
                    break
        except Exception as exc:
            close_code = status.WS_1011_INTERNAL_ERROR
            raise exc from None
        finally:
            await self.on_disconnect(websocket, close_code)

    async def _redis_setup(self, websocket) -> None:
        self.channel_layer = RedisLayer(REDIS_PATH) # could be read from the app scope.
        await self.channel_layer.subscribe_to_redis(
            websocket, receive_callback=self.on_redis_receive
        )

    async def on_redis_receive(self, websocket, data):
        # can be overidden by the user for preventing access to specific clients since all clients are subscribed to the channel.
        
        await websocket.send_json(data)

A sample usage

# Any client connected to this endpoint would automatically receive whatever data has been published.
@app.websocket_route("/redis")
class TestRedis(RedisWebSocketEndPoint):
    async def on_receive(self, ws, data):
        if data.pop("from_server", None):
            await self.channel_layer.publish_to_redis(data)

I couldn't structure it in line with the default implementation provided by the library because most of the current constructs don't match exactly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants