loading...
Cover image for SignalR core python client (VI): Client to server streamming 3.X

SignalR core python client (VI): Client to server streamming 3.X

mandrewcito profile image Andrés Baamonde Lozano ・1 min read

Intro

Thistutorial is aligned with microsoft docs tutorial written in Javascript + C#

Server signalr

In your Hub class you only need to add the method for upload items

public async Task UploadStream(ChannelReader<string> stream)
    {
        while (await stream.WaitToReadAsync())
        {
            while (stream.TryRead(out var item))
            {
                // do something with the stream item
                Console.WriteLine(item);
            }
        }
    }

Python client

I tried to make python client similar to Javascript client. With a similar syntax and behaiviour the librarie´s learning curve will be lower for people who has read the tutorials and worked with signalr in javascript previously.

Create hub connection

from signalrcore.hub_connection_builder import HubConnectionBuilder
hub_connection = HubConnectionBuilder()\
    .with_url(server_url, options={"verify_ssl": False}) \
    .configure_logging(logging.DEBUG) \
    .with_automatic_reconnect({
            "type": "interval",
            "keep_alive_interval": 10,
            "intervals": [1, 3, 5, 6, 7, 87, 3]
        })\
    .build()
hub_connection.start()

Import and create subject

from signalrcore.subject import Subject
subject = Subject()

Manage stream

myCoolStreamItem = "some content"

# start Stream
hub_connection.send("UploadStream", subject)

# call next any time you need to upload a item
subject.next(myCoolStreamItem)

# stop stream
subject.complete()

Links

Github
Pypi

Thank you for reading, and write any thought below :D

Discussion

pic
Editor guide