Using SignalR Server from Python Code

What are my options for integrating Python with SignalR?

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

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

+6
source share
2 answers

I can think of several ways, and they are all theoretical (and probably bad ideas to start with):

  • IPC - host the python application and the signalr application as different processes and somehow transfer information back and forth using a kind of ipc mechanism (named pipes, tcp, something else).
  • Use IronPython (which is probably not an option).
  • Put a lightweight SignalR check on python ( https://github.com/davidfowl/SignalR.Lite ). This probably won't work, but maybe you don't need all the features.

Or you can hope to find a library in python that does something similar.

+1
source

There is a SignalR client in the python package index named "signalr-client" that supports some of the basic SignalR "Source" functions

It is compatible with Python v2 and v3.

Supported features include:

  • Connecting to a SignalR Hub
  • Call SignalR Method
  • SignalR notification event handler

To do this, install the following modules through pip:

  • gevent
  • sseclient
  • WebSocket Client

Using the example by reference:

#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() 
+3
source

Source: https://habr.com/ru/post/947474/


All Articles