DEV Community

Cover image for SignalR core python client (VI): Client to server streamming 3.X
Andrés Baamonde Lozano
Andrés Baamonde Lozano

Posted on

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

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);
            }
        }
    }
Enter fullscreen mode Exit fullscreen mode

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()
Enter fullscreen mode Exit fullscreen mode

Import and create subject

from signalrcore.subject import Subject
subject = Subject()

Enter fullscreen mode Exit fullscreen mode

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()
Enter fullscreen mode Exit fullscreen mode

Links

Github
Pypi

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

Top comments (0)