user3607109
user3607109

Reputation: 1

Error when i try to publish an event in the client method from the hub using asp.net core's signalR library

Am using asp.net core signalR library to fetch real-time data from the server. I'am using aurelia and typescript on the client side.

On the client side receive method, am publishing an event, so that anytime the data changes on server-side, the subscribed event fetches the data without a page re-fresh.

Here is my client-side code:

this.connection = new signalR.HubConnectionBuilder()
    .withUrl("http://localhost:5003/chat")
    .configureLogging(signalR.LogLevel.Debug)
    .build();

    this.connection.start().then(()=>{ 

        this.connection.on("ReceiveMessage", (event, message) => { 

            console.log("got the message=>",event);
            this.EventAggregator.publish(event, JSON.parse(message));  

    });
    this.connection.invoke("SendMessage").catch(err => console.error(err.toString()));

}); 

on the server-side i have:

 public class ChatHub : Hub
{
    public async Task SendMessage()
    {
        System.Console.WriteLine("in send message");

        string data = "[{\"Id\":6,\"Count\":0},"+
               "{\"Id\":5,\"Count\":0}]";

        await Clients.All.SendAsync("ReceiveMessage","event1",data);
    }
}

When i run it, am getting the following error on the console:

Uncaught TypeError: Cannot read property 'publish' of undefined
at HubConnection.eval (signalRservice.ts:16)
at eval (HubConnection.js:378)
at Array.forEach (<anonymous>)
at HubConnection.invokeClientMethod (HubConnection.js:377)
at HubConnection.processIncomingData (HubConnection.js:314)
at WebSocketTransport.HubConnection.connection.onreceive (HubConnection.js:141)
at WebSocket.webSocket.onmessage (WebSocketTransport.js:171)
(anonymous) @ signalRservice.ts:16
(anonymous) @ HubConnection.js:378
HubConnection.invokeClientMethod @ HubConnection.js:377
HubConnection.processIncomingData @ HubConnection.js:314
HubConnection.connection.onreceive @ HubConnection.js:141
webSocket.onmessage @ WebSocketTransport.js:171

I'am losing the hub connection even before event is getting published.

can someone please tell me what am I doing wrong here? Am I publishing the event at the wrong place?

Upvotes: 0

Views: 416

Answers (1)

rowellx68
rowellx68

Reputation: 11

It seems that EventAggregator is not initialised. May it be that it hasn't been auto injected or manually initialised before the usage?

@autoinject
export class ClientHub {
    constructor(private eventAggregator: EventAggregator) {
    }
}

The EventAggregator should then be initialised by Aurelia's DI.

Upvotes: 1

Related Questions