1

I have a requirement to show email notification to receivers using SignalR. I am developing my application using MVC 4. I am a beginner and do not have much knowledge for SignalR. So far I have followed the posts below and tried to prepare a sample that can fulfill my requirement.

http://www.codeproject.com/Articles/732190/Real-Time-Web-Solution-for-Chat-by-MVC-SignalR-H

http://techbrij.com/realtime-post-comment-notifications-signalr-knockout

One of above is using knockout.js and other is not. I have followed both and am not able to meet my requirement.

Here is the code from SignalR Hub class:

  public void SendEmail(Email email)
        {
            email.SentBy = WebSecurity.CurrentUserId;

            using (NotifyEntities db = new NotifyEntities())
            {
                UsersContext uc = new UsersContext();
                db.Emails.Add(email);
                db.SaveChanges();

                var ret = new
                {
                    Message = post.Message,
                    SentBy = post.SentBy,
                };

                Clients.Caller.sendEmail(ret);


                #region add to table which contain user id and email_post_id (I am sending the email to 2 users/receivers)

                UsersMessage msgRel = new UsersMessage();
                msgRel.EID = email.Id;
                msgRel.UID = 2; //user 1
                db.UsersMessages.Add(msgRel);
                db.SaveChanges();


                msgRel = new UsersMessage();
                msgRel.EID = email.Id;
                msgRel.UID = 5;//user 2
                db.UsersMessages.Add(msgRel);
                db.SaveChanges();

                #endregion


                var unread = (from ure in db.Emails.ToList()
                              join um in db.UsersMessages on ure.Id equals um.EID
                              where um.UID == WebSecurity.CurrentUserId && um.IsReaded == false

                              orderby ure.Id descending

                              select new
                              {
                                  Message = pst.Message,
                                  SentBy = pst.SentBy,
                              }).ToArray();


                Clients.All.loadUnreadPosts(unread); //this returns unread emails of currently logged in user
                Clients.All.loadPosts(ret); // this returns all emails of currently logged in user

            }
        }

When I do not use knockout.js then I am not able to show instant notification, it only appears on page refresh and it display to all users instead of the receivers.

When I use knockout.js the notification is showing instantly but the same issue exists that message/notification is displaying to all users.

Please help me with this. I don't know how to create group for receivers of particular email.

NMathur
  • 829
  • 1
  • 17
  • 35

1 Answers1

3

In order to send the notification to specific users only, you would want to use the Group functionality of SignalR. Here is some documentation on how to set it up: link

You would probably end up creating a group per user on user login or some similar action.

public async Task UserOnline(string emailAddress)
{
    await Groups.Add(this.Context.ConnectionId, emailAddress);
}

Then you would want to use something like this in your signalr SendEmail hub class.

Clients.Group(emailAddress).loadUnreadPosts(unread); 
Clients.Group(emailAddress).loadPosts(ret); 

As a side note, I recommend going through the documentation at the link I provided. There should be no reason that you need to use knockout to get signalR to work appropriately.

EDIT

I have gotten something similar to the above working before (although I have lost that code); but I just came across this article that may actually provide a more elegant solution.

drneel
  • 2,887
  • 5
  • 30
  • 48
  • thank for reply, can you please share how this Context.ConnectionId work here. Actually I tried to use this but in is not same on page reload or refresh. How should I use this? – NMathur Aug 26 '14 at 11:23
  • 1
    Info on the context property. I've also updated the code to the way that you will probably access the context http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-server#contextproperty – drneel Aug 26 '14 at 12:40
  • @drneel - Any suggestion for [this](http://stackoverflow.com/q/34090045/2404470) – Zameer Ansari Dec 11 '15 at 09:03