I am trying to build a websocket. In this websocket i have two operation. İf client send me "orderbook" input, i am returning data from redis - (channel-1). If client send me "ticker" input, i am returning data from redis - (channel-2).
I can use this websocket but it's not working when one operation is online. For example if i connected with "ticker" input it's ok. But after that i cant get any operations with my websocket. I want the use 2 operation in same time or same operation in same time. But it's only working with 1 operation.
This is my websocket script
import asyncio import json import websockets import redis import yaml
with open('config.yaml') as f:
config = yaml.load(f, Loader=yaml.FullLoader)
r = redis.Redis(host=config["redis_host"], port=config["redis_port"], db=0, password=config["redis_password"])
rr = redis.Redis(host=config["redis_host"], port=config["redis_port"], db=0, password=config["redis_password"])
p = r.pubsub()
pp = rr.pubsub()
p.subscribe(config["redis_channel_name_orderbook"])
pp.subscribe(config["redis_channel_name_ticker"])
async def hello(websocket):
data = await websocket.recv()
if data == "orderbook":
while True:
message = p.get_message()
if message:
if type(message["data"]) == bytes:
message["data"] = bytes.decode(message["data"])
message = json.dumps(message["data"])
await websocket.send(message)
elif data == "ticker":
while True:
message = pp.get_message()
if message:
if type(message["data"]) == bytes:
message["data"] = bytes.decode(message["data"])
# message = json.dumps(message["data"])
await websocket.send(message["data"])
async def main():
async with websockets.serve(hello, config["redis_host"], config["websocket_port"]):
await asyncio.Future() # run forever
if __name__ == "__main__":
asyncio.run(main())
What could be wrong with this script ?