Skip to content

Instantly share code, notes, and snippets.

@Wh1t3Fox
Last active December 16, 2020 01:25
Show Gist options
  • Save Wh1t3Fox/eb5356063778db1b10296962890aae21 to your computer and use it in GitHub Desktop.
Save Wh1t3Fox/eb5356063778db1b10296962890aae21 to your computer and use it in GitHub Desktop.
# consumer.py
# https://yeti.co/blog/establishing-a-websocket-pubsub-server-with-redis-and-asyncio-for-the-light-sensor/
import asyncio
from aioredis import create_connection, Channel
import websockets
async def publish_to_redis(msg, path):
# Connect to Redis
conn = await create_connection(('localhost', 6379))
# Publish to channel "lightlevel{path}"
await conn.execute('publish', 'lightlevel{}'.format(path), msg)
async def server(websocket, path):
try:
while True:
# Receive data from "the outside world"
message = await websocket.recv()
# Feed this data to the PUBLISH co-routine
await publish_to_redis(message, path)
await asyncio.sleep(1)
except websockets.exceptions.ConnectionClosed:
print('Connection Closed!')
if __name__ == '__main__':
# Boiler-plate for the websocket server, running on localhost, port 8765
loop = asyncio.get_event_loop()
loop.set_debug(True)
ws_server = websockets.serve(server, 'localhost', 8765)
loop.run_until_complete(ws_server)
loop.run_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment