Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Signalr to have a facebook like notification system

I want to implement a facebook like notification system in ASP.NET MVC 3 : notifications are sent to a specific user to notify him for an action on one of his items.

Is signalr suited for such requirement? How could i send a notification to a specific user (all opened sessions of this user) using SignalR?

Edit

Ok, Here what i did

In the client side

$(function () {
// Proxy created on the fly
var chat = $.connection.chat;
var username = '@Html.ViewContext.HttpContext.User.Identity.Name';
// Declare a function on the chat hub so the server can invoke it
chat.addMessage = function (message) {
     $('#messages').append('<li>' + message + '</li>');
};
// Start the connection
$.connection.hub.start(function (){
    chat.join(username);
});

});

In the server side

public class Chat : Hub
{
    public void Join(string username)
    {
        AddToGroup(username);
    }
}

And every time i need to notify a user in the controller i do the following:

IConnectionManager connectionManager = AspNetHost.DependencyResolver.Resolve<IConnectionManager>();
dynamic clients = connectionManager.GetClients<Chat>();
clients[username].addMessage("test");
like image 300
hichn Avatar asked Feb 27 '12 16:02

hichn


2 Answers

Yes, SignalR is a good choice for that. Take a look at the documentation regarding Hubs (server and JS client).

You need to implement the server logic to associate your client's session with SignalR's session. You can use groups to notify all the open sessions of each user.

like image 106
nmat Avatar answered Sep 21 '22 22:09

nmat


It is appropriate for this or you use polling, those are the two choices.

Heres a brand new video from today on this:

http://channel9.msdn.com/Shows/Web+Camps+TV/Damian-Edwards-and-David-Fowler-Demonstrate-SignalR?utm_source=dlvr.it&utm_medium=twitter

like image 23
Adam Tuliper Avatar answered Sep 23 '22 22:09

Adam Tuliper