Using SignalR server from Python code

Den picture Den · Jun 17, 2013 · Viewed 10.5k times · Source

What are my options for integrating Python with SignalR?

The Python code is a part of large 3rd party product, not a matter of language preference. SignalR server provides subscriptions to existing .NET products.

We would like to reuse .NET SignalR server with Python.

Answer

Malcolm McCaffery picture Malcolm McCaffery · Nov 3, 2015

There is a SignalR client available on the python package index named "signalr-client" which supports some basic SignalR functionality Source

It is compatible with Python v2 and v3.

Functionality supported includes:

  • Connecting to SignalR Hub
  • Invoking SignalR Method
  • Event handler to process SignalR Notifications

It requires the following modules installed via pip:

  • gevent
  • sseclient
  • websocket-client

Sample usage as per link:

#create a connection
connection = Connection(url, session)

#start a connection
connection.start()

#add a handler to process notifications to the connection
connection.handlers += lambda data: print 'Connection: new notification.', data

#get chat hub
chat_hub = connection.hub('chat')

#create new chat message handler
def message_received(message):
    print 'Hub: New message.', message

#receive new chat messages from the hub
chat_hub.client.on('message_received', message_received)

#send a new message to the hub
chat_hub.server.invoke('send_message', 'Hello!')

#do not receive new messages
chat_hub.client.off('message_received', message_received)

#close the connection
connection.close()