SignalR core python client: A simple Chat Hub Client

Andrés Baamonde Lozano on February 24, 2019

Intro To end this Sunday I decided to publish a library where I have been working both at work and at home. As soon as i can. I will inc... [Read Full]
markdown guide
 

any idea why I am receving handshake error

DEBUG:SignalRCoreClient:-- web socket error --
Handshake status 400 Bad Request
ERROR:SignalRCoreClient:Handshake status 400 Bad Request

server_url = input_with_default('Enter your server url(default: {0}): ', "ws://mypc.domin.com:4500/faces")

'faces' is the hub name
endpoints.MapHub("faces");

without 'faces' it returns OK

thank you

 

Hi,

When you connect to a websocket. Previously, you negotiated the connection at mypc.domin.com:4500/faces/negotiate with a POST request. The response of that request will result a connection id that is passed on querystring of the ws://mypc.domin.com:4500/faces request.

If you are not sure that you are connecting to your hub. Try to override on your hub the following method:

public override async Task OnConnectedAsync ()

 

all the time I was testing using IIS hosted hub, but I tested now running from Visual Studio, and I got my python code working fetching the info from the hub. the issue seems related to IIS. when testing again using IIS hub, I am getting again the handshake error

 

thank you Andrés Baamonde Lozano for your reply

curl -XPOST mypc.domain.com:4500/faces/negotiate -H 'Content-Length:0'
{"connectionId":"4Qwmg1Q1jsGN-5-gGpmbPw","availableTransports":[{"transport":"ServerSentEvents","transferFormats":["Text"]},{"transport":"LongPolling","transferFormats":["Text","Binary"]}]}

but I am when using the sample code to have python code connected to the Hub I am getting the following error:
DEBUG:SignalRCoreClient:Handler registered started broadcast
DEBUG:SignalRCoreClient:Connection started
DEBUG:root:start url:ws://mypc.domain.com:4500/faces
DEBUG:SignalRCoreClient:-- web socket error --
Handshake status 400 Bad Request
ERROR:SignalRCoreClient:Handshake status 400 Bad Request
DEBUG:SignalRCoreClient:-- web socket close --

any idea please if I have missed something?

 

Great work!
One question, if I want to store the json data coming in through hub_connection.on("ReceiveMessage", print), in a txt file, how would I do that?

 

Yes, you can, 'print' is only the callback function. If you create a function like this:

import uuid
import json
def save_message(msg):
    with open(f"{uuid.uuid4()}.json",'w+') as fh:
        fh.write(json.dumps(msg))

Then you only need to replace 'print' on the callback register function:

hub_connection.on("ReceiveMessage", save_message)

This changes will save data passed trhoung signalr

 
 

I am trying to make a service of this chat example, which will run at boot up using systemd but it fails with import: not found on all the imports in the program.

How can I make it execute as a service?

 

Have you venv? if answer is yes try to execute with full path to your python venv´s interpreter.

 

How would I go about reconnecting in case of websocket disconnect?

 

On disconnect event is only logged, but HubConnection inherits from websocket.WebSocketApp you can search a solution for you problem in this library. Now i have free time to work in library again, so asap i try to fix this issue. Here is an issue related github.com/websocket-client/websoc...

 

I have looked at the link. The only thing I see is to add run_forever, but you already have that in base_hub_connection.

run_forever seems to work when network cable is disconnected but it only tries to reconnect for approx 1 minute, and it also gives a websocket error after 1 min.

This weekend i was working on it, during this week. I will update library with "ondisconnected" method to configure reconnection. I tried to do that simplest way. I will explain the implementation on a post, to see if someone gives me feedback and improve that feature.

 
 

thank you very much for your effort you put on this.

  1. is it ok to use it for commercial apps?
  2. I noticed that the package does not support https?
 

I supports https on the latest version(github.com/mandrewcito/signalrcore...) and yes you can use it! any colaboration is welcomed :D

 
code of conduct - report abuse