signalrcore: Python SignalR Core full client (transports and encodings). Compatible with azure / serverless functions. Also with automatic reconnect and manually reconnect. · Pythie
Python SignalR Core full client (transports and encodings). Compatible with azure / serverless functions. Also with automatic reconnect and manually reconnect.
Maintained.Last release 2 weeks ago, commits in 3 of the last 26 weeks. Weaker on commit activity and contributor spread.
Test environment has as a requirement a signalr core server, is available in here
Clone repos and install virtual environment:
git clone https://github.com/mandrewcito/signalrcore-containertestservers
cd signalrcore
make dev-install
git clone https://github.com/mandrewcito/signalrcore-containertestservers
cd signalrcore-containertestservers
docker compose up
cd ../signalrcore
make tests
Have fun :)
A Tiny How To
You can reach a lot of examples in tests folder, raw implementations in playground and fully working examples at the examples folder.
A login function must provide an error controller if authorization fails. When connection starts, if authorization fails exception will be propagated. Exceptions raised by the token factory are passed through unchanged; negotiate rejections raise UnAuthorizedHubError for HTTP 401 and HubError for any other non-200 status or invalid body.
def login(self):
response = requests.post(
self.login_url,
json={
"username": self.email,
"password": self.password
},verify=False)
if response.status_code == 200:
return response.json()["token"]
raise requests.exceptions.ConnectionError()
hub_connection.start() # this code will raise requests.exceptions.ConnectionError() if auth fails
on_open - fires when connection is opened and ready to send messages
on_close - fires when connection is closed
hub_connection.on_open(lambda: print("connection opened and handshake received ready to send messages"))
hub_connection.on_close(lambda: print("connection closed"))
On Hub Error (Hub Exceptions ...)
hub_connection.on_error(lambda data: print(f"An exception was thrown closed{data.error}"))
Register an operation
ReceiveMessage - signalr method
print - function that has as parameters args of signalr method
hub_connection.on("ReceiveMessage", print)
Sending messages
SendMessage - signalr method
username, message - parameters of signalrmethod
stream() requires a running transport. If the connection is not started, stopped, or reconnecting, it raises HubConnectionError and does not register the stream handler. send() and invoke() behave the same way.
Client side Streaming
from signalrcore.subject import Subject
subject = Subject()
# Start Streaming
hub_connection.send("UploadStream", subject)
# Each iteration
subject.next(str(iteration))
# End streaming
subject.complete()
AIO
The AIO client is an asyncio-native implementation built with the standard library. It supports WebSockets, Server-Sent Events and Long Polling, JSON and MessagePack, authentication, proxies, TLS, automatic reconnection and streaming. Every network operation is a coroutine, and callbacks may be synchronous or asynchronous.
await hub.start() waits until the handshake completes for at most AIO_CONNECTION_TIMEOUT seconds (30 by default). Negotiation and the transport connection have their own bounds, and await hub.stop() bounds shutdown with AIO_CLOSE_TIMEOUT (10 seconds by default). If a stage does not complete, TimeoutError is raised instead of hanging. await hub.stop() is safe to call before start(). The state can also be awaited explicitly:
from signalrcore.transport.base_transport import TransportState
await hub.wait_until_state(TransportState.connected, timeout=10)
Transports and encoding
The negotiated WebSockets transport is used by default. To force another transport, pass HttpTransportType in the options:
Use MessagePackHubProtocol for binary encoding. MessagePack requires a transport that supports the Binary transfer format, such as WebSockets or Long Polling:
Event, lifecycle, invocation and stream callbacks can be plain functions or coroutine functions. Coroutine callbacks are awaited before message processing continues:
Client-to-server streaming uses AIOSubject. Its operations are coroutines:
from signalrcore.aio.subject import AIOSubject
subject = AIOSubject()
await hub.send("UploadStream", subject)
for i in range(10):
await subject.next(str(i))
await subject.complete()
The synchronous signalrcore.subject.Subject is not compatible with asyncio connections; passing it raises a clear TypeError.
Authentication and proxies
access_token_factory may be a normal function or a coroutine function. The token is fetched before connecting and sent as a bearer token:
Plain HTTP proxy requests use the absolute-form target, while HTTPS/WSS connections use CONNECT tunnelling followed by TLS. Use verify_ssl: False for local development or pass a custom ssl_context for certificate configuration.