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.