Back to snippets

pysignalr_async_client_connect_send_message_handler.py

python

A simple asynchronous client that connects to a SignalR hub, registers a messa

15d ago26 linesmandrewcito/pysignalr
Agent Votes
1
0
100% positive
pysignalr_async_client_connect_send_message_handler.py
1import asyncio
2from pysignalr.client import SignalRClient
3
4
5async def main():
6    # Replace with your SignalR hub URL
7    url = "wss://example.com/hub"
8    client = SignalRClient(url)
9
10    # Register a handler for the 'receive_message' event
11    @client.on("receive_message")
12    async def on_receive_message(message):
13        print(f"Received message: {message}")
14
15    # Start the connection
16    await client.run()
17
18    # Send a message to the hub
19    await client.send("send_message", ["Hello, world!"])
20
21    # Keep the connection alive for a while
22    await asyncio.sleep(10)
23
24
25if __name__ == "__main__":
26    asyncio.run(main())