How to implement SignalR to show email notifications to receivers in ASP.NET MVC 4 Application

NMathur

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.

drneel

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Toast notifications in ASP.NET MVC 4

From Dev

how to send an email in asp.net mvc4

From Dev

how to implement globalization in ASP.NET MVC 4?

From Dev

Using WebGrid to show a list of email addresses (strings) - ASP.Net MVC4

From Dev

How to run Asp.net Webforms Website with MVC 4 application

From Dev

Deploy ASP.NET MVC 4 Application

From Dev

How can I send email using Gmail SMTP in asp.net mvc application?

From Dev

How to display push notifications using SignalR in MVC

From Dev

How to Pass the Email Id value after checking Captcha in Asp.Net Mvc4?

From Dev

How should I implement SAMLP 2.0 in an ASP.NET MVC 4 service provider?

From Dev

ASP.NET MVC4 Get email of logged user

From Dev

How to set up IIS6 to host ASP .NET 4 MVC4 application

From Dev

How do i show a list from a model to another View in ASP.NET MVC 4

From Dev

How to show a specific html elements for specific authorized users in ASP.NET MVC 4

From Dev

Show password checkbox in ASP.NET MVC 4

From Dev

Sending SignalR Broadcasts that is running within ASP.NET MVC Application from a Windows Service on the same server

From Dev

Why is HTTPContext.Current.Session null using SignalR 2.x libraries in a ASP .Net MVC application?

From Dev

Send notification to web client from external service via SignalR hub hosted in Asp.Net MVC Application

From Dev

How to override Browser.IsMobileDevice is ASP.NET MVC4 application?

From Java

How to add Web API to an existing ASP.NET MVC 4 Web Application project?

From Dev

How to Deploy asp.net mvc 4 application having multiple Areas

From Dev

How to easily apply Bootstrap to a completed ASP.net MVC 4 application?

From Dev

How should I share TSQL scripts in an ASP.NET MVC 4 web application project using (localDB)?

From Dev

How to deploy ASP.NET MVC 4 application using localDB to local IIS on Windows 7?

From Dev

How to migrate an asp.net webform and mvc 4 application to AngularJS site

From Dev

How to force Motorola 2180 mobile computer to be recognized as mobile device in ASP.NET MVC4 application

From Dev

How to run a asp.net MVC4 web application in RedHat OS

From Dev

How to Deploy asp.net mvc 4 application having multiple Areas

From Dev

How do I import d3 (v4) into typescript in an ASP.NET MVC Application?

Related Related

  1. 1

    Toast notifications in ASP.NET MVC 4

  2. 2

    how to send an email in asp.net mvc4

  3. 3

    how to implement globalization in ASP.NET MVC 4?

  4. 4

    Using WebGrid to show a list of email addresses (strings) - ASP.Net MVC4

  5. 5

    How to run Asp.net Webforms Website with MVC 4 application

  6. 6

    Deploy ASP.NET MVC 4 Application

  7. 7

    How can I send email using Gmail SMTP in asp.net mvc application?

  8. 8

    How to display push notifications using SignalR in MVC

  9. 9

    How to Pass the Email Id value after checking Captcha in Asp.Net Mvc4?

  10. 10

    How should I implement SAMLP 2.0 in an ASP.NET MVC 4 service provider?

  11. 11

    ASP.NET MVC4 Get email of logged user

  12. 12

    How to set up IIS6 to host ASP .NET 4 MVC4 application

  13. 13

    How do i show a list from a model to another View in ASP.NET MVC 4

  14. 14

    How to show a specific html elements for specific authorized users in ASP.NET MVC 4

  15. 15

    Show password checkbox in ASP.NET MVC 4

  16. 16

    Sending SignalR Broadcasts that is running within ASP.NET MVC Application from a Windows Service on the same server

  17. 17

    Why is HTTPContext.Current.Session null using SignalR 2.x libraries in a ASP .Net MVC application?

  18. 18

    Send notification to web client from external service via SignalR hub hosted in Asp.Net MVC Application

  19. 19

    How to override Browser.IsMobileDevice is ASP.NET MVC4 application?

  20. 20

    How to add Web API to an existing ASP.NET MVC 4 Web Application project?

  21. 21

    How to Deploy asp.net mvc 4 application having multiple Areas

  22. 22

    How to easily apply Bootstrap to a completed ASP.net MVC 4 application?

  23. 23

    How should I share TSQL scripts in an ASP.NET MVC 4 web application project using (localDB)?

  24. 24

    How to deploy ASP.NET MVC 4 application using localDB to local IIS on Windows 7?

  25. 25

    How to migrate an asp.net webform and mvc 4 application to AngularJS site

  26. 26

    How to force Motorola 2180 mobile computer to be recognized as mobile device in ASP.NET MVC4 application

  27. 27

    How to run a asp.net MVC4 web application in RedHat OS

  28. 28

    How to Deploy asp.net mvc 4 application having multiple Areas

  29. 29

    How do I import d3 (v4) into typescript in an ASP.NET MVC Application?

HotTag

Archive