Created
August 4, 2023 21:02
-
-
Save nishanc/a2c7f3a843028233da328ab858730387 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import * as signalR from '@microsoft/signalr'; | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class SignalRService { | |
private hubConnection: any; | |
public startConnection(){ | |
return new Promise((resolve, reject) => { | |
this.hubConnection = new signalR.HubConnectionBuilder() | |
.withUrl('https://localhost:7007/notification') | |
.build(); | |
this.hubConnection | |
.start() | |
.then(() => { | |
console.log("Connected to SignalR Hub >>>"); | |
return resolve(true); | |
}).catch((error: any) => { | |
console.error(error); | |
reject(error); | |
}) | |
}); | |
} | |
public listenToNotifications(callback: (message: string) => void): void { | |
this.hubConnection.on('SendNotificationAsync', (message: string) => { | |
callback(message); | |
}); | |
} | |
public sendNotification(message: string): void { | |
this.hubConnection.invoke('SendNotificationToUserAsync', message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment