DEV Community

Cover image for SignalR core python client (IV): Improving syntax
Andrés Baamonde Lozano
Andrés Baamonde Lozano

Posted on • Updated on

SignalR core python client (IV): Improving syntax

As Loongle (github user) suggested into this issue placed on github. Library syntax should migrate to an approach similar to javascript.

    this.hubConnection = new signalR.HubConnectionBuilder()
      .withUrl('http://localhost:55563/imessage/chart', {
        accessTokenFactory: () => this.tokenService.get().token
      }).build();

Enter fullscreen mode Exit fullscreen mode
hub_connection = HubConnectionBuilder()
    .with_url(server_url, {
        "access_token_factory": lambda : tokenfactory().token
        })
    .build()
Enter fullscreen mode Exit fullscreen mode

Library examples obviously will change, and it will look like :

Chat

hub_connection = HubConnectionBuilder().with_url(server_url).build()
hub_connection.on("ReceiveMessage", print)
hub_connection.start()
Enter fullscreen mode Exit fullscreen mode

Chat with auth

hub_connection = HubConnectionBuilder()\
    .with_url(server_url, {
        "access_token_factory": lambda: signalr_core_example_login(login_url, username, password)
    })\
    .build()

hub_connection.on("ReceiveSystemMessage", print)
hub_connection.on("ReceiveChatMessage", print)
hub_connection.on("ReceiveDirectMessage", print)
hub_connection.start()
Enter fullscreen mode Exit fullscreen mode

Streamming

hub_connection = HubConnectionBuilder().with_url(server_url).build()
hub_connection.start()
time.sleep(10)
hub_connection.stream(
    "Counter",
    [10, 500]).subscribe({
        "next": lambda x: print("next callback: ", x),
        "complete": lambda x: bye(False, x),
        "error": lambda x: bye(True, x)
    })

Enter fullscreen mode Exit fullscreen mode

I will include this changes and the catch block soon (Im open to any suggest). Im doing this change before message pack, It´s a tiny change i prefer doing this changes now because changes in basic syntax too late are a pain.

The sense of this post its a question. What kind of syntax do you prefer?. Placing this on the next´s version library .. Its a good decision in next migration? Changes probably will come this weekend so post below if you think that im not on the right way (:

ToDo

  • message pack

Links

Github
Pypi

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

Discussion (0)