13 April 2018

Angular 4/5 & SignalR connection


Signal R is one of the most widely used 

Step1:install i @aspnet/signalr-client --save
Step 2: include needed import in your component.

import { HubConnection } from '@aspnet/signalr-client';

now in your component

//HUB connection
this.connection = $.hubConnection(this.signalRURL);
this.connection.logging = true;
this.proxy = this.connection.createHubProxy('HUBNAME');

this.proxy.on('broadcastMessage', (data: any) => {
var msg = (JSON.parse(data)); Alert(msg); });
this.connection.start().done((data: any) => {
console.log(data.id);
}).catch((error: any) => {
this.connectionStatus = "Connection Failed";
console.log('Hub error -> ' + error);
});


 

No comments:

Post a Comment

Thanks for your valuable comments

Rate Now: