user5326354
user5326354

Reputation:

Integrate signalr with web api to push notifications to clients

I develop an application, server side technology is asp.net web api, client side is javascript. In my application, I use Signalr to notify clients of updates - push notifications from server to clients.

All the signalr interaction happens outside of the Hub class, due to the pushes coming from HTTP requests and not from a client push to the server through signalr.

After proccessing a successful HTTP request I want to let all the clients beside the one who comitted the request to be notified.

Who should be responsible for those notifications? After processing a legal request, should my server just push a notification to the clients? (This is what I do at the moment) if this is the way to go how can I notify all the clients besides the one who made the request? Reminder - because the request was Http and not signalr push, the pushes to client side happens in a class outside of the Hub. Therefore I can't use the Context, or the Client.Others property as you would have probably suggested. How than could I know which clients should be notified? At the moment I just use Clients.All and the one who made the request is also notified which I don't like.

Option 2 is that after my server responses with a success status code. The client who made the request will be responsible to notify the other clients - meaning he will push message to the server, and the server will than push that notification to all the other clients (this time it will be within signalr Hub so I could use Clients.Others).

Which approach should I take?

Upvotes: 7

Views: 8465

Answers (1)

Mark C.
Mark C.

Reputation: 6450

I think your first approach is seamless and would be fairly easy to implement. Plus, you don't want users in charge of notifications. Users are lazy.

So basically after you've 'processed' your request and you haven't caught any exceptions, you'll get your SignalR Hub Context and use the built-in SignalR C# API to broadcast a message to every Clients except the sender.

Here's some code that would go into your Controller:

    [Route("ProcessMessage")]
    public async void ProcessMessage(string[] clientConnectionStrings)
    {
        // do some processing
        var hub = GlobalHost.ConnectionManager.GetHubContext<MessagingHub>();

        hub.Clients.AllExcept(clientConnectionStrings);
    }

You'll have to pass in the Client's connection ID to use AllExcept but it should be a fairly easy integration. (Note: that if you were interacting directly with your SignalR Hub you wouldn't have to do this)

Note: To get the Client's connection to exclude from your broadcast, you can access it like :

$.connection.hub.id

Another approach to this could be using the SignalR Hub directly, but would require additional JavaScript code to trigger events on the Hub Server. For example:

    $.ajax({
        url: 'Some/Url/',
        type: 'POST',
        success: function (data) { 
                connection.server.someDataChangeEvent(); 
        },
        error: function (ex) { return; }
    });

SignalR Hub Code :

public void SomeDataChangeEvent(string connId)
{
    Clients.Others.updateData(_someDataStore.getUpdates().ToList());
}

Client Code :

connection.client.updateData = function() {
    //do stuff
}

Upvotes: 2

Related Questions