SignalR core python client (VII): Azure
Tags: signalrcore, python, client, signalr
Published: 2020-02-16 19:24:12

---
title: SignalR core python client (VII): Azure
published: true
description: Signalr core library now dealing with client to server stream
tags: signalrcore, python, client, signalr
series: SignalR core python client
cover_image: https://www.campusmvp.es/recursos/image.axd?picture=banner-asp-net-core-750x300.png
---

# Intro

These days i have been working on my library, mainly on improve log. But in adition i had work on integration with azure and and serverless functions. This will be a short post, because microsoft tutorials are really good and library integration has no breaking changes. So here is a how-to use both.  

## Local server with Azure signalr

Here you can read a guide of [how to create a signalr resource on azure](https://docs.microsoft.com/es-es/azure/azure-signalr/signalr-quickstart-dotnet-core). On this option you only need to stablish a connection with our local server. It will log througth azure and return a url. With that url we can connect to azure signalr. This will occur automatically, user has nothing to do. local server and library do all work. Implementation is like a normal chat. This code can be checked [here](https://mandrewcito.github.io/signalrcore/)

```python
import logging

from signalrcore.hub_connection_builder import HubConnectionBuilder

# Create custom handler
handler = logging.StreamHandler()
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)

# build connection ...
hub_connection = HubConnectionBuilder() \
        .with_url(server_url, options={
            "verify_ssl": False,
            "headers": {
            }
        }) \
        .configure_logging(logging.DEBUG, socket_trace=True, handler=handler) \
        .with_automatic_reconnect({
            "type": "interval",
            "keep_alive_interval": 10,
            "intervals": [1, 3, 5, 6, 7, 87, 3]
        }).build()

```

## Azure functions

Other implementation choice throught azure is a serverles function. With this  [Tutorial](https://docs.microsoft.com/es-es/azure/azure-signalr/signalr-quickstart-azure-functions-javascript?WT.mc_id=signalrquickstart-github-antchu) you can implement. Tutorial has also a tiny web code in html/javascript, with this you can verify that your serverless function is working properly.

Client is a bit different, messages are sended with an http post  instead of of sending it through the socket.


```python
import logging
import sys
import requests
from signalrcore.hub_connection_builder import HubConnectionBuilder


def input_with_default(input_text, default_value):
    value = input(input_text.format(default_value))
    return default_value if value is None or value.strip() == "" else value


server_url = input_with_default(
    'Enter your server url(default: {0}): ',
    "localhost:7071/api")
username = input_with_default('Enter your username (default: {0}): ', "mandrewcito")
handler = logging.StreamHandler()
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
hub_connection = HubConnectionBuilder() \
        .with_url("ws://"+server_url, options={
            "verify_ssl": False,
            "access_token_factory": lambda: "",
            "headers": {
            }
        }) \
        .configure_logging(logging.DEBUG, socket_trace=True, handler=handler) \
        .build()

hub_connection.on_open(lambda: print("connection opened and handshake received ready to send messages"))
hub_connection.on_close(lambda: print("connection closed"))

hub_connection.on("newMessage", print)
hub_connection.start()
message = None

# Do login

while message != "exit()":
    message = input(">> ")
    if message is not None and message is not "" and message is not "exit()":
        # hub_connection.send("sendMessage", [username, message])
        requests.post("http://localhost:7071/api/messages", json={"sender": username, "text": message})

hub_connection.stop()

sys.exit(0)
```

this changes will be realsed on the new version of library 0.8.4


## Links

[Github](https://github.com/mandrewcito/signalrcore)
[Pypi](https://pypi.org/project/signalrcore/)

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