4

I have a web site where many registered users are connected on the same time and they can send messages one to each other. Each user has his own Session object on server and in this Session object I store "Messages" list - this are the User's messages.

Session["Messages"] = new List<UserMesages>();
//.... more code here
Session["Messages"] = BL.GetMyMessages();  //get a list of messages
//.... more code here
BL.MessageReceived += RefreshUserMessages();

public static void RefreshUserMessages(){
    //this code doesn't execute for all the sessions, but only for my current user.
}

I need a way to know when the user received a new message - I don't want on each request to interrogate my database.

I tried to use an event in by BL classes (when a message is stored in database, an event is raised), but the events don't pass from one Session to other Session.

In my BL class I have:

public delegate void MessageReceivedEventHandler(object sender, MessageEventArgs e);
public static event MessageReceivedEventHandler MessageReceived;

public static int SaveMessageForUser(Message msg) {
    //... save the message
    if(MessageReceived != null) {
        MessageReceived (userid, msg)  // <-- correct parameters go here
    }
}

Any idea? How to implement, in ASP.Net such

2 Answers 2

5
+50

I recommend that you try to rather approach this problem with SignalR. SignalR is the perfect component for building a real-time chat application with minimal effort.

It will allow you to do everything you want to do.

Checkout this tutorial:

http://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr

Sign up to request clarification or add additional context in comments.

1 Comment

Yea, we are implemented messaging system via SignalR
1

I'd probably approach this using the Application -- add some kind of tracking table or list, or a set of tables or lists depending, to which you add the user ID when they are sent a message from another user, and remove it when the messages are checked. That would void any need to interrogate the database, and should provide a simple means for the system to identify if a message has been received for that user.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.