0 0
WP Ad Inserter plugin
Read Time:5 Minute, 45 Second

SignalR provides the ability to push or broadcast information from a back-end server at any time to any front end web application that has subscribed. This allows for real-time updates of data on web sites, multiplayer games, polls, interactive chat bots and various other uses.

Initially tied to ASP.net and jQuery, SignalR has been around for a long time however much has changed in recent years. Now the technology can be used serverless with a whole range of front end frameworks and a range of back ends languages including Javascript, python, java and C#.

Other features also include two way communication, broadcasting notifications to groups and client auto reconnection.

How does it work

SignalR achieves bi-directional communication using a range of client and server technologies. In HTML5 it uses web sockets however where this is not possible it will communicate using long polling.

All communication is done through the SignalR hub. Previously this would have been as part of an ASP.net application or more recently on an ASP.net core application. Now we are able to create a SignalR hub inside Azure as a stand-alone resource meaning it can be used with a serverless application and integrated into Azure functions.

A Sample Application

There are many types of applications you can create using SignalR. To show how SignalR works however I have chosen the simplest possible application with one-way communication from server to all subscribed clients.

In this application when a file is dropped into azure blob storage this will trigger an Azure function. The function will broadcast through the serverless SignalR Hub to any subscribed clients that a new file is now available.

The sample code is using Azure functions in JavaScript and a simple ReactJS front end however the concepts are the same with other languages.

Creating the Hub

Login to the Azure portal and select SignalR to create a new resource.

There are a number of fields to complete:

Pricing tier

The pricing tier option can be set to either standard or free. Free has a few limits such as limiting concurrent connected users to 20, however is great to get started. Full pricing can be found on the pricing guide.

Units

SignalR units refer to what is in affect a node or sub instance. To improve performance on a standard plan you can have only 1,000 concurrent connections per unit. It is intended that you use multiple units to overcome this limit. A more detailed description on units can be found here on Stack Overflow.

Service mode

For working with Azure functions this must be set to Serverless

Under keys you will find the connection string you will need later. Nothing else needs to be configured in the portal.

Negotiate Function

When the website begins a SignalR connection it will need to locate a HTTP triggered function named negotiate.

This is very simple and contains a single line. For this demo we will use Javascript however the code for other languages can be found in the official documentation

1module.exports = async function (context, req, connectionInfo) {

2 context.res.body = connectionInfo;

3};

Now you have a function you will need to configure the bindings to include SignalR as below. The AzureSignalRConnectionString can be set in your function app settings using the value from the portal.

1{

2 "bindings": [

3 {

4 "authLevel": "anonymous",

5 "type": "httpTrigger",

6 "direction": "in",

7 "name": "req",

8 "methods": [

9 "get",

10 "post"

11 ]

12 },

13 {

14 "type": "http",

15 "direction": "out",

16 "name": "res"

17 },

18 {

19 "type": "signalRConnectionInfo",

20 "name": "connectionInfo",

21 "hubName": "serverless",

22 "connectionStringSetting": "AzureSignalRConnectionString",

23 "direction": "in"

24 }

25 ]

26}

Blob Triggered Function

For this sample application we use a blob trigger and broadcast to all subscribers when a new file is added.

1module.exports = async function (context, myBlob) {

2

3 context.bindings.signalRMessages = [{

4 "target": "newFile",

5 "arguments": [ context.bindingData.blobTrigger, context.bindingData.uri ]

6 }];

7

8 context.done();

9};

In the code the target refers to the method name in the web application and arguments is an array of the method parameters that are passed in to it.

To complete this function we need to set the function bindings to include SignalR, the Blob trigger and set the connection string in the function settings.

1{

2 "bindings": [

3 {

4 "name": "myBlob",

5 "type": "blobTrigger",

6 "direction": "in",

7 "path": "media",

8 "connection": "StorageConnectionString"

9 },

10 {

11 "type": "signalR",

12 "name": "signalRMessages",

13 "hubName": "serverless",

14 "connectionStringSetting": "AzureSignalRConnectionString",

15 "direction": "out"

16 }

17 ]

18}

Client Web Application

The client web application code is all in a single file and can be viewed on GitHub along with the rest of the code. For this section I have selected only the SignalR section of the code to talk over.

You will need to run npm install @microsoft/signalr. Be aware there are other SignalR npm packages for integrations with asp.net and other implementations.

1import React from 'react';

2import * as signalR from '@microsoft/signalr'

We will set an array of files in the react state so we can update it when a new file notification arrives. When the page has loaded (componentDidMount in react), we connect to SignalR and setup the call backs.

For the rest of the file including the render methods see Git Hub.

1componentDidMount()

2 {

3

4

5

6 const apiBaseUrl = "http://localhost:7071/api";

7

8 var connection = new signalR.HubConnectionBuilder()

9 .withUrl(apiBaseUrl)

10 .build();

11

12

13

14 connection.start().then(function () {

15 console.log("connected");

16 }).catch(function (err) {

17 return console.error(err.toString());

18 });

19

20

21

22 connection.on("newfile", (name,uri) => {

23 let { files } = this.state;

24 files.push({name:name, uri: uri });

25 this.setState({ files: files });

26 });

27 }

Once the application is running, both the functions and the UI, adding a new file to blob storage will then display in the web browser without the user having to refresh.

Azure Web PubSub

Azure Web PubSub is a newer Azure offering that is currently still in preview. Azure Web PubSub can achieve the same result however works at a lower level. This means you miss out on some of the features of SignalR such as auto re-connection and communication full backs. I plan to write a full post on this later with a similar sample application.

Summary

SignalR has come a long way and can now be used with a whole range of languages making it very versatile and simpler to consume.

In this post we looked at Azure SignalR and it’s possible uses. We created a simple application that allows clients to subscribe to the SignalR hub and be notified whenever a new file was added to the blob storage. If you wish to see the full sample the code can be found on GitHub.

Title Photo by Markus Winkler on Unsplash.

Source: https://www.blueboxes.co.uk/getting-started-with-serverless-signalr-and-azure-functions

Code opinion: performance or clean code? Previous post Code opinion: performance or clean code?
Reviewing Craft Nitro & DevKinsta for WordPress Local Development Next post Reviewing Craft Nitro & DevKinsta for WordPress Local Development

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.