1

Currently I am creating a chat. I want to use AspNetCore (2.2) SignalR. I have followed the instructions on https://learn.microsoft.com/en-us/aspnet/core/tutorials/signalr?view=aspnetcore-2.2&tabs=visual-studio but now I am facing the following issue: When I run my client in IIS the following error occurs:

ReferenceError: signalR is not defined[Learn More] script.js

anonymous http://localhost:20610/js/script.js

I searched the web and found out that in NodeJS you have to use const signalR = require("@aspnet/signalr"); But in my case I am using plain javascript so I thought I don't need to add this line. Am I right?

Here is my html and the beginning of my js code:

<head>
    <title>abc</title>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="style/style.css">
    <script src="js/jquery-3.3.1.min.js"></script>
    <script src="js/signalr.min.js"></script>
    <script src="js/script.js"></script>
</head>
var connection = new signalR.HubConnectionBuilder().withUrl(base).build();

Are there any suggestions how to get this working?

Thanks :)

CLRW97
  • 470
  • 4
  • 15
  • 1
    Is `signalr.min.js` in the `js` folder? Press F12 in web browser to see whether there is any other error before `signalR is not defined[Learn More] script.js` – Edward Jan 28 '19 at 01:52
  • The correct answer can be found here: https://stackoverflow.com/a/54340966/8492915 – SmallWorld Aug 31 '23 at 11:16

3 Answers3

2

SignalR is not defined, so it's not found. Despite the fact you program in plain JavaScript, you still have to include the SignalR. Also, make sure that the path is actually leading to SignalR.

Brent Meeusen
  • 179
  • 1
  • 11
  • 1
    First of all thank you very much for you suggestion. That's the first time I include an external js-file so I would appreciate if you could be a little bit more precise of how to do that. I have tried it with this line: `import signalR from "./signalr.js";` but now I get an Uncaught SyntaxError: Unexpected identifier. What am I doing wrong? I also tried adding `import signalR from "@aspnet/signalr";` with the same error. – CLRW97 Jan 27 '19 at 14:08
  • I just tried a few things and saw that the log of the API shows a signalR connection when I load my website. Then I tried my chat and it seems that signalR works with the statements formulated in the question, even though the error "signalR is not defined" occurs. Therefore the problem is not so important anymore but still I would like to know how to eradicate this error. – CLRW97 Jan 27 '19 at 15:44
1

I'm a few months late(new and can't comment either), however, I had the same issue. Not seeing a response here, I figured I'd add one. To clear the error, move your <script src="~/lib/signalr/dist/browser/signalr.js"></script> to the Pages/Shared_Layout.cshtml file and place it somewhere, such as under the footer.

This should remove the warning in the console also.

1

for me this worked

<script src="https://cdnjs.cloudflare.com/ajax/libs/microsoft- signalr/5.0.8/signalr.min.js"></script>