To facilitate real-time communication between a Node.js API and a .NET API, particularly for the scenario of updating a WhatsApp device QR code every 20 seconds using the text library on the Node.js side and consuming this in a .NET API. and signalR throwing error on connection in nodejs
Here is the Node Js code
const express = require("express");
const {HubConnectionBuilder, LogLevel,HttpTransportType} = require("@microsoft/signalr");
const app = express();
const connection = new HubConnectionBuilder()
.withUrl("https://localhost:7094/chathub")
.withAutomaticReconnect()
.configureLogging(LogLevel.Trace)
.build();
async function start() {
try {
await connection.start();
console.log("SignalR Connected");
} catch (err) {
console.log(err);
}
};
start();
app.listen(3000, () => {
console.log('Server started on http://localhost:3000');
});
Program.Cs
using WA_API;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddCors();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddSignalR();
var app = builder.Build();
app.UseSwagger();
app.UseSwaggerUI();
app.UseCors(cp => cp
.AllowAnyHeader()
.WithOrigins("http://localhost:3000")
.AllowCredentials()
);
app.MapHub<ChatHub>("/chathub");
app.MapControllers();
app.Run();
Here is the error message
[2024-03-13T07:41:34.522Z] Debug: Starting HubConnection.
[2024-03-13T07:41:34.525Z] Debug: Starting connection with transfer format 'Text'.
[2024-03-13T07:41:34.526Z] Debug: Sending negotiation request: https://localhost:7094/chathub/negotiate?negotiateVersion=1.
Server started on http://localhost:3000
[2024-03-13T07:41:34.632Z] Warning: Error from HTTP request. TypeError: fetch failed.
[2024-03-13T07:41:34.632Z] Error: Failed to complete negotiation with the server: TypeError: fetch failed
[2024-03-13T07:41:34.633Z] Error: Failed to start the connection: Error: Failed to complete negotiation with the server: TypeError: fetch failed
[2024-03-13T07:41:34.633Z] Debug: HubConnection failed to start successfully because of error 'Error: Failed to complete negotiation with the server: TypeError: fetch failed'.
I have tried every solution I can find. like tried http and https and SkipNegotiation and cors policy.