signalR : /signalr/hubs is not generated
Asked Answered
O

9

23

I can get this tutorial to work in a new project, but not in my existing project.

My project is an ASP.Net MVC 4 web application with the following attribute in the web.config file:

<appSettings>
  <add key="webpages:Enabled" value="true"/>
</appSettings>

This is because my application is a Single-Page-Application, which uses AngularJS on the client side. The only page in my application is index.cshtml, to which I've added the relevant code for signalR:

 <!-- signalR chat -->
<script src="~/Scripts/jquery.signalR-1.0.0.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="/signalr/hubs"></script>
<!--Add script to update the page and send messages.--> 
<script type="text/javascript">
    $(function () {
        // Declare a proxy to reference the hub. 
        var chat = $.connection.chatHub;
        // Create a function that the hub can call to broadcast messages.
        chat.client.broadcastMessage = function (name, message) {
            // Html encode display name and message. 
            var encodedName = $('<div />').text(name).html();
            var encodedMsg = $('<div />').text(message).html();
            // Add the message to the page. 
            $('#discussion').append('<li><strong>' + encodedName
                + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>');
        };
        // Get the user name and store it to prepend to messages.
        $('#displayname').val(prompt('Enter your name:', ''));
        // Set initial focus to message input box.  
        $('#message').focus();
        // Start the connection.
        $.connection.hub.start().done(function () {
            $('#sendmessage').click(function () {
                // Call the Send method on the hub. 
                chat.server.send($('#displayname').val(), $('#message').val());
                // Clear text box and reset focus for next comment. 
                $('#message').val('').focus();
            });
        });
    });
</script>

Then I've got the ChatHub.cs file:

public class ChatHub : Hub
{
    public void Send(string name, string message)
    {
        // Call the broadcastMessage method to update clients.
        Clients.All.broadcastMessage(name, message);
    }
}

And finally in the global.asax:

 protected void Application_Start()
    {
        RouteTable.Routes.MapHubs();
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }

When I run the application, the /signalr/hubs file is not generated. I get a 404 when requesting the file, and it crashes on the line:

 chat.client.broadcastMessage = function (name, message) { ....

because chat is null as the previous line did not find chatHub:

var chat = $.connection.chatHub;

Does anyone know what's wrong with my code ?

UPDATE

I have solved my problem by changing the line::

<script src="/signalr/hubs"></script>

to

<script src="~/signalr/hubs"></script>
Oberstone answered 2/7, 2013 at 9:29 Comment(0)
O
20

I have solved my problem by changing the line::

<script src="/signalr/hubs"></script>

to

<script src="~/signalr/hubs"></script>
Oberstone answered 2/7, 2013 at 10:1 Comment(7)
BTW, /signalr/hubs is not a file in the project. In case anyone is tempted to waste time looking for it. If you launch the app then go to that url it will show it to you.Gash
@Gash , what you mean its not a file ? , i developed an real time notification system and its not invoking $.connection.hub.start() . I think ~/signalr/hubs has to do something with it cause its nowhere in solution even after adding the packageLeveille
@Gash , sorry for posting comment because i dont wanna post same question again , My root directory is localhost:56547/Home/Index, are you suggesting to go to localhost:56547/signalr/hubs ??Leveille
@Gash yes , it isLeveille
@toddmo, i tried checking with url (localhost:50877/signalr/hubs). It gives me resource not found error page. can you help me out. Because of this I think my " var chat = $.connection.chatHub; " returns a null. Can you please help me out ?Drillstock
@t4thilina, please post a new question and remember to include your code. Thanks!Gash
@toddmo, i posted it in this link... #46499718. please do help me out. Thank you. :)Drillstock
P
8

Also, the reason why /signalr/hubs are not generated is forget to Map SignalR in OWIN Startup Configuration.

public class Startup
{
   public void Configuration(IAppBuilder appBuilder){
         ...
         appBuilder.MapSignalR();
         ...
   }
 ...
Personification answered 17/6, 2015 at 11:18 Comment(0)
G
2

In my case, it was because my ChatHub class was not marked public.

Godfrey answered 28/11, 2015 at 21:35 Comment(0)
E
1

I had a similar problem where the hubs file wasn't being generated. It looks like the OP was following the steps here. The way I fixed the problem had to do with the jquery includes. The tutorial I linked below was written with jquery 1.6.4 and jquery-signalr version 2.1.0. When Visual Studio generated the Scripts folder for me, it used jquery version 1.10.2 and jquery-signalr version 2.0.2.

The way I fixed this was simply to edit the index.html file. Note that you can use Chrome's javascript console window Ctrl+Shift+J to see errors.

Emlyn answered 2/2, 2015 at 15:3 Comment(0)
S
1

For me the solution was to reinstall all the packages and restore all the dependecies.

Open nuget powershell console and use this command.

Update-Package -Reinstall
Salesclerk answered 27/5, 2017 at 9:50 Comment(1)
Thanks - this is what I was looking for! The OP was about a scenario that had not yet ever worked due to incorrect/incomplete setup....but if ones signalR/hubs request starts suddenly returning a 404 - this answer may do the trick for ya! Telltale symptom: works on other dev machines but not yours.Pasol
O
1

Some advice for those that start scaling out from the get go. In my case I was working on getting a remote client to work and never realized that

A. the tutorial example lists the web app(server) startup in a using statement, after which the web app disposes properly and no long exists.

I got rid of the using statement and keep a reference to the web app for later disposal

B. the client has a different url than the server. the example relies on them having the same url. the "/signalr/hubs" is an endpoint run by the signalr server, called by the signalr client to get a script of all the hubs the server implements.

You need to include "http://myServerURL/signalr/hubs", rather than making it relative to the client.

No lies. This tripped me up for a solid 2 weeks, because by some magic the solution worked anyways on my coworker's setup. This caused me to keep looking for IIS settings and firewall settings and CORS settings that must have been blocking the connection on my computer. I scavenged every last stack overflow question I could and the ultimate answer was that I should have just implemented a heartbeat monitor on the web app from the start.

Good luck and hopefully this saves other people some time.

Ophthalmoscope answered 5/5, 2020 at 15:19 Comment(1)
Why am i being downvoted? this answer explains the real reason that asp.net implementations require extra work and is applicable to all uses of Signalr, rather than exclusively the poster's implementation.Ophthalmoscope
B
0

I'll like to add that the signalR Readme file have some note about this issue. And also if your signalR page is in a PartialView some script should be place in the master page.

Please see http://go.microsoft.com/fwlink/?LinkId=272764 for more information on using SignalR.

Upgrading from 1.x to 2.0
-------------------------
Please see http://go.microsoft.com/fwlink/?LinkId=320578 for more information on how to 
upgrade your SignalR 1.x application to 2.0.

Mapping the Hubs connection
----------------------------
To enable SignalR in your application, create a class called Startup with the following:

using Microsoft.Owin;
using Owin;
using MyWebApplication;

namespace MyWebApplication
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.MapSignalR();
        }
    }
} 

Getting Started
---------------
See http://www.asp.net/signalr/overview/getting-started for more information on how to get started.

Why does ~/signalr/hubs return 404 or Why do I get a JavaScript error: 'myhub is undefined'?
--------------------------------------------------------------------------------------------
This issue is generally due to a missing or invalid script reference to the auto-generated Hub JavaScript proxy at '~/signalr/hubs'.
Please make sure that the Hub route is registered before any other routes in your application.

In ASP.NET MVC 4 you can do the following:

      <script src="~/signalr/hubs"></script>

If you're writing an ASP.NET MVC 3 application, make sure that you are using Url.Content for your script references:

    <script src="@Url.Content("~/signalr/hubs")"></script>

If you're writing a regular ASP.NET application use ResolveClientUrl for your script references or register them via the ScriptManager 
using a app root relative path (starting with a '~/'):

    <script src='<%: ResolveClientUrl("~/signalr/hubs") %>'></script>

If the above still doesn't work, you may have an issue with routing and extensionless URLs. To fix this, ensure you have the latest 
patches installed for IIS and ASP.NET. 
Byssus answered 10/4, 2017 at 14:25 Comment(0)
E
0

In my case i was missing :

        app.MapSignalR();

in public void Configuration(IAppBuilder app) function located in startup.cs

Exhaust answered 21/3, 2019 at 17:35 Comment(0)
P
0

See if Microsoft.Owin.Host.Systemweb nuget package is not installed, and therefore not building the dynamic js lib.

OwinStartup not firing

Pout answered 20/9, 2022 at 11:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.