Last active
December 16, 2020 01:25
-
-
Save Wh1t3Fox/eb5356063778db1b10296962890aae21 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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