How to hook up SignalR with an Angular 7 application

ε祈祈猫儿з 提交于 2019-12-03 12:56:49

I just spent two days trying to figure out this same thing. I finally got it to work and these are the few things that i had to do:

1) You noted that using the @aspnet/signalr package was incorrect for .Net framework, and that is correct. You need the signalr package (npm install signalr).

2) This is the most critical part of the whole process. SignalR has a dependency on jQuery. You have to include jQuery before including the signalr script. In the angular.json file, under the scripts section, you need to include:

"./node_modules/jquery/dist/jquery.js", "./node_modules/signalr/jquery.signalR.js"

in that exact order. On start up of your project, it will load jQuery first, then the signalR script.

Many other stackover flow answers answering the question in reply to this error:

jQuery was not found. Please ensure jQuery is referenced before the SignalR client JavaScript file

tell you to write import * as $ from "jquery" in the component you are wanting to use jQuery in. However, it is NOT correct to do this. The reason is, according to this angular article about global scripts, using the import syntax will include it in the module load and put it in the vendor.js file that is created from running an ng build command. The reason this is a problem is because jQuery will get loaded first from your angular.json, then signalR will be loaded, then the module from the vendor.js will RELOAD jQuery and unattach all the events that were just attached to jQuery from signalR.

3) Since you noticed you were using the .Net Core version of signalr, you wont have access to the HubConnectionBuilder when trying to instantiate a new HubConnection in your angular component.

Instead, when the signalr script gets executed, it will attach additional events to the $ in your code. Note: if you get errors on build or in compile time from your ts files, make sure you've included the @types/signalr and @types/jquery from npm

To set up a new hub connection, use $.hubConnection("your-url-here/signalr"). This will attach to your server's Hub when running. Note: I stored the result from this as a variable called hubConnection in my angular component

On your server code (.cs file), where you have your Hub class, you will need to add above the class name: [HubName("YourHubName")]. So in your case your .cs file would look something like this at the top:

[HubName("Broadcast")]    
public class BroadcastHub : Hub

You will most likely have to include this at the top of your .cs file: using Microsoft.AspNet.SignalR.Hubs;

Then in your Angular Component you set up a proxy to attach to that Hub on your server The very next line after instantiating your new hubConnection, write:

this.hubConnection.createHubProxy("yourHubName");.

In your case, this.hubConnection.createHubProxy("broadcast");

After you have made your proxy, you can then attach listeners to listen to events emitted from the server, or you can invoke server functions from your angular components.

I followed this example here to learn how to set up calling events and listening to server events. Yes it is angular 2, but the functions from signalr all still work the same in my angular 7 app.

Short answer: use proxy.on('eventname') to listen to events from the server, and use proxy.invoke('eventname') to call functions on your Hub from your angular components.

Lastly, a few notes in your cs files. In my Startup.cs, the only thing i have for mapping signalr is app.MapSignalR(). I did not go into many details regarding other properties to set like you have done, but that may be another cause of some issues?

Was just researching this subject myself, and found npm package ng2-signal. Maybe something to look into yourself?

Are you sure that the hubEndpoint is correct? It seems like the hub is part of the angular routing (judging by the way you are writing it). Try to set the full path (ex. https://www.myapp.com/SynBroadcastHub/BroadcastMessage)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!