You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SignalR code is given below. Its consistently sending notifications to the client all the time. However after deploying it in Windows Server 2012 SP2 with load balanced, sticky session enabled servers, its not always sending the notifications to the caller consistently. Its works sometimes, fails most of the time.
Not sure what's going wrong. Any help is much appreciated. I have enabled client side trace, and attached the log with this ticket.
Thanks,
Mohan
SignalRService.ts
import { Injectable, EventEmitter } from '@angular/core';
import { HubConnection, HubConnectionBuilder, LogLevel } from '@microsoft/signalr';
import { APP_CONFIG } from '@app/shared/Services/app-config.service';
import { AuthService } from '@app/core/auth.service';
const DEFAULT_TIMEOUT_IN_MS: number = 120 * 1000;
const DEFAULT_PING_INTERVAL_IN_MS: number = 45 * 1000;
@Injectable({
providedIn: 'root',
})
export class SignalrService {
private hubConnection: HubConnection | undefined;
public Notification: EventEmitter;
constructor(private readonly authService: AuthService) {
this.Notification = new EventEmitter();
}
public initialize(): Promise {
return new Promise((resolve, reject) => {
this.stopConnection();
const token = this.authService.accessToken;
this.hubConnection = new HubConnectionBuilder()
.withUrl(`${APP_CONFIG.BudgetAPIUrl}/notification`, {
headers: { Authorization: `Bearer ${token}` },
withCredentials: true,
accessTokenFactory: () => {
return token;
},
})
.withAutomaticReconnect() // Tries to reconnect 4 times before giving up
.configureLogging(LogLevel.Trace)
.build();
this.hubConnection.serverTimeoutInMilliseconds = DEFAULT_TIMEOUT_IN_MS;
this.hubConnection.keepAliveIntervalInMilliseconds = DEFAULT_PING_INTERVAL_IN_MS;
this.hubConnection.on('GetNotification', (data) => {
this.Notification.emit(data);
});
this.hubConnection
.start()
.then(() => {
console.log(`SignalR connection success! connectionId: ${this.hubConnection.connectionId}`);
// Sending a dummy message to initiate the communication with the Server Hub Method
// In future if required it can be used to send messages to other users using as shown below
// this.hubConnection.send('SendNotification', 'Test') // where Test is the message to be sent to all
this.hubConnection.invoke('SendNotification').catch((error) => {
console.log(`NotificationHub.SendNotification() error: ${error}`);
alert('NotificationHub.SendNotification() error!, see console for details.');
});
resolve(true);
})
.catch((error: any) => {
console.log(`SignalR connection error: ${error}`);
setTimeout(() => this.initialize(), 3000);
reject(false);
});
});
Client Consumer in ngOnInit():
this.signalrService.Notification.subscribe((result: string) => {
const parsedResult = JSON.parse(result);
if (parsedResult !== null) {
Server Hub Code:
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;
namespace MyWeb.WebAPI.Hubs
{
[Authorize]
public class NotificationHub : Hub
{
public void SendNotification()
{
// Method intentionally left empty.
// Currently its used to start commnucation from Client to make sure SignalR Hud connection is fine
}
/*
// SigralR doesn't support overloading,
// the below method shows how to send data from client to server for two way communication
public async Task SendNotification(string data)
{
await Clients.All.GetNotification(data);
}
*/
}
I have enabled client side trace, and attached the log with this ticket.
You didn't share any logs. Your HAR just shows normal traffic with a few signalr connections, nothing odd (except that you keep making new connections).
You haven't attached any logs, you attached a network trace which didn't show anything except normal SignalR traffic.
Please give a detailed explanation of "its not always sending the notifications" and provide client and server logs and a network trace when the issue happens.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi,
SignalR code is given below. Its consistently sending notifications to the client all the time. However after deploying it in Windows Server 2012 SP2 with load balanced, sticky session enabled servers, its not always sending the notifications to the caller consistently. Its works sometimes, fails most of the time.
Not sure what's going wrong. Any help is much appreciated. I have enabled client side trace, and attached the log with this ticket.
Thanks,
Mohan
SignalRService.ts
import { Injectable, EventEmitter } from '@angular/core';
import { HubConnection, HubConnectionBuilder, LogLevel } from '@microsoft/signalr';
import { APP_CONFIG } from '@app/shared/Services/app-config.service';
import { AuthService } from '@app/core/auth.service';
const DEFAULT_TIMEOUT_IN_MS: number = 120 * 1000;
const DEFAULT_PING_INTERVAL_IN_MS: number = 45 * 1000;
@Injectable({
providedIn: 'root',
})
export class SignalrService {
private hubConnection: HubConnection | undefined;
public Notification: EventEmitter;
constructor(private readonly authService: AuthService) {
this.Notification = new EventEmitter();
}
public initialize(): Promise {
return new Promise((resolve, reject) => {
this.stopConnection();
const token = this.authService.accessToken;
}
stopConnection() {
if (this.hubConnection) {
this.hubConnection.stop();
this.hubConnection = null;
}
}
}
Client Consumer in ngOnInit():
this.signalrService.Notification.subscribe((result: string) => {
const parsedResult = JSON.parse(result);
if (parsedResult !== null) {
Server Hub Code:
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;
namespace MyWeb.WebAPI.Hubs
{
[Authorize]
public class NotificationHub : Hub
{
public void SendNotification()
{
// Method intentionally left empty.
// Currently its used to start commnucation from Client to make sure SignalR Hud connection is fine
}
}
Controller:
_hub.Clients.All.GetNotification(JsonConvert.SerializeObject(response));
Beta Was this translation helpful? Give feedback.
All reactions