Can't connect to Azure signalR

Gerald Benavente 0 Reputation points
2025-02-08T10:59:44.1933333+00:00

User's image

I can't connect to azure signalR,

Here's my code.

startup.cs

 string connectionString = System.Configuration.ConfigurationManager.AppSettings["SignalRConn"];

 ConfigureAuth(app);
 app.MapAzureSignalR(this.GetType().FullName, connectionString);
 GlobalHost.TraceManager.Switch.Level = SourceLevels.Information;

Hub

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;

namespace CNMS.Nextvas.App
{
    public class AlertHub : Hub
    {
        public override Task OnConnected()
        {
            return base.OnConnected();
        }
        public void BroadcastMessage(string name, string message)
        {
            Clients.All.broadcastMessage(name, message);
        }
        public override Task OnDisconnected(bool stopCalled)
        {
            return base.OnDisconnected(stopCalled);
        }
    }
}

javascript:
User's image

 var chat = $.connection.alertHub;
 // Create a function that the hub can call to broadcast messages.
 chat.client.broadcastMessage = function (name, message) {
     // Html encode display name and message.
     var encodedName = $('<div />').text(name).html();
     var encodedMsg = $('<div />').text(message).html();
     // Add the message to the page.
     console.log(encodedName + " " + encodedMsg);
 };
 // Get the user name and store it to prepend to messages.
 // Set initial focus to message input box.
 // Start the connection.
 $.connection.hub.start().done(function () {
     $('#test-btn-signalr').click(function () {
         // Call the Send method on the hub.
         chat.server.broadcastMessage("test", "test 1");
         // Clear text box and reset focus for next comment.
     });
 });

Azure SignalR Service
Azure SignalR Service
An Azure service that is used for adding real-time communications to web applications.
156 questions
0 comments No comments
{count} votes

Accepted answer
  1. Siva Nair 150 Reputation points Microsoft Vendor
    2025-02-12T08:58:51.5733333+00:00

    Hello Gerald Benavente,

    Welcome to the Microsoft Q&A and thank you for posting your questions here.

    I understand that you're experiencing difficulties connecting to Azure SignalR.

    To fix the issue, Lets follow these steps:

    1. Check Azure SignalR Connection String - Ensure that the connection string (signalRConn) in your (app.config) or (web.config) is correct. Verify that the connection string is valid and matches the one provided in the Azure portal.
    2. Ensure Azure SignalR Service is Running Go to the Azure portal and check if the Azure SignalR Service instance is running. Ensure that the service is not in a "Disabled" state.
    3. Enable Logging for Debugging Add logging to your SignalR client and server to identify where the issue might be. On the server side, you've already set the trace level to information. Check logs for any errors. On the client side, enable logging in JavaScript.
    4. Check CORS and Network Issues- if your client and server are hosted on different domains, ensure that CORS is properly configured. Check the browser's developer console (F12) for any network errors or CORS-related issues.
    5. Verify Hub Configuration Ensure that your (AlertHub) class is correctly registered and accessible. The JavaScript client should reference the hub name correctly:
    6. Check Azure SignalR SDK Version Ensure that you are using the latest version of the Azure SignalR SDK. Update the NuGet packages: Microsoft.Azure.SignalR (Please add) Microsoft.AspNet.SignalR (that you have already added,just check for update once)
    7. Check Azure Firewall and Network Settings- ensure that your Azure SignalR Service allows connections from your application's IP address. Check if there are any firewall rules or network security groups (NSGs) blocking the connection.

    Keynote-

    Error - As we are getting "Failed to connect" error.

    Fix area- Network issue, firewall, or DNS problem.

    If you have any further assistant, do let me know.
    If the answer is helpful, please click Accept Answer and kindly upvote it so that other people who faces similar issue may get benefitted from it.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.