superfurryanimals
superfurryanimals

Reputation: 177

Is it possible to use SignalR outside of a Microsoft Environment?

I currently have an API solution created with WebApi that hosts a Hub. When I create an MVC project inside of this solution SignalR works correctly and as expected.

I have a separate project with no Microsoft infrastructure associated with it aside from a sole reference to jquery.signalR-2.0.2.js

When I reference in this project it is not found (my understanding is that this is auto-generated). Is there a way I can use signalR in my separate HTML/JS solution or am I limited to creating my projects inside the same solution?

Upvotes: 1

Views: 547

Answers (2)

Pontus Ivarsson
Pontus Ivarsson

Reputation: 126

Sure you can. Download/save or generate a proxy file. To generate install the Microsoft.AspNet.SignalR.Utils NuGet package. Include the generated proxy (js script) in your none MS client project.

If your client is deployed under different domainname, then the hub endpoint, configure support for cross-domain requests.

If JSONP is required on the client (to support cross-domain requests in older browsers), it will need to be enabled explicitly by setting EnableJSONP on theHubConfiguration object to true. JSONP is disabled by default, as it is less secure than CORS.

Upvotes: 2

Yes it is possible on the client side. I am working on an AngularJS web application that uses SignalR v2.0.2 using the jquery.signalR-2.0.2.js reference.

On the server side, SignalR needs OWIN to startup using these lines of code in the Startup.cs:

public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // SignalR Hub Startup
            var hubConfiguration = new HubConfiguration();
            hubConfiguration.EnableDetailedErrors = true;
            hubConfiguration.EnableJavaScriptProxies = true;
            hubConfiguration.EnableJSONP = false;

            app.MapSignalR(hubConfiguration);

        }
    }

Then a javascript file will be auto-generated on the server-side and can be accessed by the client using a tag in the HTML document. Here is what is needed on the client-side for setting up the hub:

<script src="Scripts/jquery-1.10.2.min.js"></script>
<script src="Scripts/jquery.signalR-2.1.0.min.js"></script>
<script src="signalr/hubs"></script>

The last script line simply is a reference to the auto-generated javascript created by the server-side. If you open this URL in your browser you will see the script generated that will be accessed by the non-.NET web client.

More info here: ASP.NET SignalR Hubs API Guide - JavaScript Client

Upvotes: 2

Related Questions