Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR - The signalr/hubs file

Tags:

signalr

Can somebody explain to me how the signalr/hub file works?

After reading through the documentation it says the file is generated at run time. However when i first load a site, you include it with all your other javascript files such as

<script src="Scripts/jquery-1.10.2.min.js"></script>
<!--Reference the SignalR library. -->
<script src="Scripts/jquery.signalR-2.1.2.min.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="signalr/hubs"></script>

How does that work?

I can not find that file on disk at all, so how is the web browser able to request it like that?

Thanks for any explanations!

like image 265
CBaker Avatar asked Dec 19 '22 19:12

CBaker


2 Answers

Like the documentation says, the JS is auto generated. The file itself won't be stored on the file system, but rather generated on request (probably cached) to the /signalr/hubs location.

Type in http://myServer:port/signalr/hubs into your browser and you'll see what the js file looks like.

The principle is the same as with every web application: the exact file you're seeing when you navigate to a page isn't a copy of some file from a file system, but rather it is the result of the server code running. Installing SignalR sets up a route to handle requests made to /signalr/hubs and when requested will return the string that makes up the JS file.

This code is configured to run when you add the SignalR dlls to your project and prepare your server to use SignalR when you do the following statement, usually in Startup.cs or whatever your Owin startup class is.

app.MapSignalR();
like image 107
DLeh Avatar answered Jan 02 '23 08:01

DLeh


<!--Reference the autogenerated SignalR hub script. -->
<script src="signalr/hubs"></script>

The Documentation explains that it a autogenerated file, that means this file actually does not exist in our project. Actually it is generated Runtime when a request is send to server.

 $(function () {
        // Declare a proxy to reference the hub. 
        var chat = $.connection.chatHub;

        // 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());                                       
            });
        });
    });

when the above request is sent to server the further process of server can be seen in browser using below URL as mentioned in answer by #DLeh.

http://myServer:port/signalr/hubs

also can be observed in console panel.

For further Guidance you can use the following link.

https://learn.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server

like image 29
Divya Avatar answered Jan 02 '23 09:01

Divya