Anushka Madushan
Anushka Madushan

Reputation: 681

How to have SignalR in my separate Api and mvc client project?

I'm trying to use SignalR in my client project. What I trying to do is when a method in my api executes , I want to show notification in my client side project. For that how can I set up two projects?. For more clarification. Let say I have a api method called MethodA(), when that method executes each time, the client side project should get a notification. How can I do that?

Upvotes: 4

Views: 1416

Answers (1)

Woot
Woot

Reputation: 1809

When you configure signalr in the api you setup the url that your client should listen too. In the below example if my api is http://localhost:55985 then the url my client will listen to would be http://localhost:55985/signalr

namespace MyApplication
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // Any connection or hub wire up and configuration should go here
            app.MapSignalR("/signalr", new HubConfiguration());
        }

    }
}

Then in the client project would configure it's javascript signalr connection like so

<script type="text/javascript">
    $(function () {

        var hub = $.connection.myHub;
        $.connection.hub.url = 'http://localhost:55985/signalr';

        $.connection.hub.start();
    });
</script>

Keep in mind you will likely need to allow CORS in your dev environment. I would not recommend setting up your project so that CORS is allowed in production unless that is one of your requirements. Also not include in the code is the Hub that you need to setup. That information can be found here.

Upvotes: 6

Related Questions