Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use SignalR in an Azure function app?

I'm building a small game that will be driven by web socket using SignalR on one hand and Azure function app on the other hand. Basically, the user establishes a web socket connection with the server and sends/receives a message from it. This is mainly done this way because players can discuss with each other in real-time.

Besides, I'd like to have some Azure function apps that run and execute some instructions. For example, every minute, an app would make some monsters move. If these monsters are around a specific player, I'd like him to know.

For this, I have two solutions in mind:

  • Requesting the information every second from the client and then warning the user if he needs to be.
  • Opening a connection to my web socket from within my function app to send the data and the hub would forward the information to affected users.

The first option kind of defeat the purpose of a web socket to me. What's the point of having web socket if I need to poll the server for some information.

The second option seems better but as I'm not familiar with function apps yet, I'm wondering if it's the way to go. Is it possible/correct to open a web socket connection from a function app?

Maybe there are some better options?

like image 896
ssougnez Avatar asked Aug 07 '17 19:08

ssougnez


People also ask

How does Azure SignalR work?

SignalR Service transmits data from the client to the pairing application server. And data from the application server will be sent to the mapped clients. SignalR Service does not save or store customer data, all customer data received is transmitted to target server or clients in real-time.

Is SignalR a REST API?

Azure SignalR Service provides REST API to support server to client communication scenarios, such as broadcasting. You can choose any programming language that can make REST API call. You can post messages to all connected clients, a specific client by name, or a group of clients.

How do I enable authentication in azure function app?

Enable App Service Authentication Select the subscription and function app name to open the function app in the Azure portal. In the function app that was opened in the portal, locate the Platform features tab, select Authentication/Authorization. Turn On App Service Authentication.


1 Answers

For example, every minute, an app would make some monsters move. If these monsters are around a specific player, I'd like him to know.

If you’d like to call hub method from your Azure Functions app to broadcast monsters position info to specific players, you can refer to the following sample that works fine on my side.

Hub class

public class ChatHub : Hub
{       

    public void BroadcastMonstersPosition(string MonsterPositionInfo)
    {
        Clients.All.addNewMessageToPage(MonsterPositionInfo);
    }

    //other hub methods
}  

Azure Functions app (timerTrigger)

using System;


public static void Run(TimerInfo myTimer, TraceWriter log)
{
    var hub = new Microsoft.AspNet.SignalR.Client.HubConnection("http://xxxxxx.azurewebsites.net/signalr/hubs");

    var proxy = hub.CreateHubProxy("ChatHub");
    hub.Start().Wait();

    //invoke hub method
    proxy.Invoke("BroadcastMonstersPosition", "new position info");
    log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
}

function.json

{
  "bindings": [
    {
      "name": "myTimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 */1 * * * *"
    }
  ],
  "disabled": false
}

project.json

{
  "frameworks": {
    "net46":{
      "dependencies": {
        "Microsoft.AspNet.SignalR.Client": "2.2.0"
      }
    }
   }
}

Client user can receive message that Azure Functions app send

enter image description here

Besides, if you want to broadcast to specific players instead of all connecting players, you can refer to the following code.

Clients.Clients(clientids_list).addNewMessageToPage(MonsterPositionInfo);
like image 149
Fei Han Avatar answered Oct 11 '22 08:10

Fei Han