How do I implement a broadcast server using picows? #36
Answered
by
tarasko
river-walras
asked this question in
Q&A
-
|
We assume a scenario where I have a server, and I need to broadcast the messages received to all clients, rather than having an echo server. How can this be achieved? I can implement a framework using WebSockets, but since picows creates a server that does not return transport, I cannot use transport to send messages. import asyncio
from websockets.asyncio.server import serve
async def handler(websocket):
...
async def broadcast(message):
...
async def broadcast_messages():
while True:
await asyncio.sleep(1)
message = ... # your application logic goes here
await broadcast(message)
async def main():
async with serve(handler, "localhost", 8765):
await broadcast_messages() # runs forever
if __name__ == "__main__":
asyncio.run(main()) |
Beta Was this translation helpful? Give feedback.
Answered by
tarasko
Feb 9, 2025
Replies: 1 comment 1 reply
-
|
Hi @River-Shi |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
tarasko
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @River-Shi
I've added examples on how to do broadcasting:
server
client