connect to a hub - js RRS feed

  • Question

  • User640374800 posted

    I am setting up signalr within a asp.net core project and have installed the aspnet core packages for signalr and signalr client.

    I have installed the signalr js files using add > client-side library and these reside at - /lib/signalr/dist/browser/signalr.js

    A reference to this file has been added to my _Layout partial view alongside my own message.js file.

    Startup file configured correctly and have created my signalr hub within a Hubs folder.

    The route defined works ok in a browser - /signal 

    Connection ID required

    app.UseSignalR(routes =>

    However when trying to set up the client side connection to receive messages (message.js) i get an error trying to run the app at this line...

    var connection = new signalR.HubConnectionBuilder()

    Uncaught ReferenceError: signalR is not defined

    I have gone through several guides on setting this up and cannot see anything obvious that i have not done or done wrong.

    any help appreciated...

    Wednesday, September 11, 2019 8:06 AM

All replies

  • User61956409 posted

    Hi mark1961,


    Uncaught ReferenceError: signalR is not defined

    Please try to check if signalr.js is loaded on your web page as expected using browser F12 developer tools Network.

    Besides, please try to create new page without using _Layout, then add references to jquery.js, signalr.js and message.js directly on that new page, and check if your SignalR JavaScript client code can work well.

    With Regards,

    Fei Han

    Wednesday, September 11, 2019 8:24 AM
  • User640374800 posted


    yes for some reason it creates connection when js file moved into the parent cshtml file.

    i am now not getting the client side listener responding to the Hub method - SendMessage.

    My hub is

    using Microsoft.AspNetCore.SignalR;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;

    namespace SchedulerSignalRApp.Hubs
    public class SignalRHub : Hub
    public async Task SendMessage(int number)
    await Clients.All.SendAsync("ReceiveMessage", number);

    public override async Task OnConnectedAsync()
    await Clients.All.SendAsync("UserConnected", Context.ConnectionId);
    await base.OnConnectedAsync();

    public override async Task OnDisconnectedAsync(Exception ex)
    await Clients.All.SendAsync("UserDisconnected", Context.ConnectionId);
    await base.OnDisconnectedAsync(ex);

    and its fired from a scheduler task that when periodically invoked creates a hub instance and calls the hub method.

    public async Task Invoke() // fires every 5 minutes
    var number = this._numberincrementor.IncrementNumber();
    SignalRHub hub = new SignalRHub();
    await hub.SendMessage(number);

    what i see when i debug is that the hub has null for Clients, Context and Groups after being instantiated, therefore Clients is null in the hub call

    await Clients.All.SendAsync("ReceiveMessage", number);

    so does not work.

    When i inject the hub context into my controller i do see a valid context with a connection id key. (after the js HubConnectionBuilder has run)

    My client side listener for this hub message is

    connection.on("ReceiveMessage", function (message) {
    var msg = message.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
    var div = document.createElement("div");
    div.innerHTML = msg + "<hr/>";

    Am i missing something in my hub code?

    Wednesday, September 11, 2019 1:39 PM
  • User2096382003 posted

    ok it seems from examples signalr can just be used on user actions like button clicks not events like scheduled tasks...

    the hub only accepts calls from the client...

    i will investigate an alternative way to do this

    Wednesday, September 11, 2019 7:27 PM