Skip to content

Instantly share code, notes, and snippets.

@highfestiva
Last active November 7, 2017 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save highfestiva/f4048aff880e0c952e47a68a4f690455 to your computer and use it in GitHub Desktop.
Save highfestiva/f4048aff880e0c952e47a68a4f690455 to your computer and use it in GitHub Desktop.
Chat server in 13 LoC
#!/usr/bin/env python3
import asyncio
import websockets
connections = set()
async def broadcast(websocket, path):
connections.add(websocket)
try:
async for message in websocket:
print(message)
await asyncio.wait([w.send(message) for w in connections])
finally:
connections.remove(websocket)
asyncio.get_event_loop().run_until_complete(websockets.serve(broadcast, '0.0.0.0', 5001))
asyncio.get_event_loop().run_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment