10

This is my chat javascript

"use strict";

var connection = new signalR.HubConnectionBuilder().withUrl("/chathub").build();

connection.on("ReceiveMessage", function (message) {
    var msg = message.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
    var encodedMsg = msg;
    var li = document.createElement("li");
    li.textContent = encodedMsg;
    document.getElementById("Messages").appendChild(li);
});

connection.start().catch(function (err) {
    return console.error(err.toString());
});

document.getElementById("Send").addEventListener("click", function (event) {
    var message = document.getElementById("Message").value;
    connection.invoke("SendMessage", message).catch(function (err) {
        return console.error(err.toString());
    });
    event.preventDefault();
});

This is the error I get when I run the page with my chat input:

Uncaught ReferenceError: signalR is not defined at chat.js:3

line 3 in chat.js is:

var connection = new signalR.HubConnectionBuilder().withUrl("/chathub").build();

I downloaded the SignalR client library from Visual Studio Add Client Side Library. What I have now is a file called jquery.signalR.js and it is the ASP.NET SignalR Javascript Library 2.4.0.

However, this error doesn't go away and I am unable to proceed for some reason.

TanvirArjel
  • 30,049
  • 14
  • 78
  • 114
JianYA
  • 2,750
  • 8
  • 60
  • 136

3 Answers3

20

I see your application is ASP.NET Core but you are using jquery.signalR.js which is actually for ASP.NET MVC. SignalR client for ASP.NET Core is signalr.js which is independent of jQuery. You can download the signalr.js for ASP.NET Core from NPM:

You can also download using Visual Studio Client Side Library Manager (LibMan) as follows:

  • In Solution Explorer, right-click on the project, and select Add > Client-Side Library.

  • In the Add Client-Side Library dialog, for Provider select unpkg.

  • For Library, enter @microsoft/signalr@latest, and select the latest version
    that isn't preview.

  • Must change Target Location to wwwroot/lib/signalr if it is anything else containing @ in the path. Otherwise it would not download.

enter image description here

For more details: Getting started with ASP.NET Core SignalR

TanvirArjel
  • 30,049
  • 14
  • 78
  • 114
  • Hi, is there a way to get it without using npm? Do I have to install node js on my machine? – JianYA Jan 24 '19 at 07:05
  • This is how i am including in my page, still i am getting Uncaught ReferenceError: signalR is not defined – Sameer Kamran Jun 11 '19 at 05:16
  • 1
    +1 Note that this package has since [been moved](https://stackoverflow.com/a/58711826/5405967) from `@aspnet` to `@microsoft`. – MarredCheese Jul 22 '20 at 22:15
  • This works thanks – SmallWorld Aug 31 '23 at 11:15
1

For my case i forget the order of sjgnalr.js and chat.js files.

  <script src="~/lib/signalr/dist/browser/signalr.js"></script>
    <script src="~/js/chat.js"></script>

My Mistake was as fllows:

  <script src="~/js/chat.js"></script>
      <script src="~/lib/signalr/dist/browser/signalr.js"></script>

Cause I'm not care the order of js files so SignalR is not defined is usual.

iman mohadesi
  • 109
  • 1
  • 8
1

You're using the wrong signalr javascript library. Try https://www.npmjs.com/package/@aspnet/signalr, that's what worked for me.

Amy Zheng
  • 11
  • 1