How to resolve SignalR Hub connection issue from Client side Javascript code

Mayuri Barve 1 Reputation point
2024-05-15T12:40:41.6033333+00:00

I have web application developed in .NET 5.0. This web application calls Web APIs to fetch information from SQL DB. Web and API applications are two separate applications hosted in Azure. I want to push some real time data every 2 minutes to all clients when ever any record is inserted or updated in DB. I am using SignalR in my web application to achieve this. I have added required package references on server side and signalR.js on client side. Have done all the configurations on both side.

Server side: Created a Hub class with SendMessage method. I have also used IHostedService for periodic execution and calling hub's SendMessage method from IHostedService Start event to send updated data to all Clients.

Client side: Establishing connection to the hub as

var hubConnection = new signalR.HubConnectionBuilder() .withUrl("/hub/myHub") .withAutomaticReconnect() .build() .start() .then(function () { alert('connection established!'); console.log("Hub Connection established."); }) .catch(function (err) { alert(err); return console.error(err.toString()); });; hubConnection.on("UpdateCount", (myData) => { if (myData != null) { //update value in label } });

 

When I run the application it is giving error: Failed to complete negotiation with the server: TypeError: Failed to fetch

Error is occurring in FetchHttpClient.js file. 

Can anyone please suggest what is missing here?

 

Thanks.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,467 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,246 questions
{count} votes