Oscar Jara
Oscar Jara

Reputation: 14187

How to implement jquery/javascript notifications and count from database?

I want to implement something with jquery, simple javascript and java but I don't have any idea to do this.

Description:

A) A user logs into the system and his userid is in session. I want to send a message by clicking a button to another user (identified with his userid) and, when the process finishes, on a db table will be a new record, like this:

message_id = 1; sender_user_id = xxx; recipient_user_id = yyy; message = "hello";

B) The recipient user if it's logged into the system, has to receive a notification like "Hey, you got a new message" on a div that dissapears after few seconds, also a text like "inbox(0)" has to be updated to "inbox(1)".

C) If the recipient user is not logged, when he decides to enter the system at anytime or any day, the notification has to apper and the text of "inbox(0)" have to be updated as step 2.

Notes:

So, what's the best way to do all this?

Thanks in advance.

Upvotes: 0

Views: 1829

Answers (4)

madfriend
madfriend

Reputation: 2430

If the speed of message delivery doesn't matter (e.g., instant update or a minute-late update) I would recommend polling.

The simplest way to do that is the following. On the client-side, send AJAX requests every minute/30 seconds. If response contains message (i.e., JSON response with message property), update user interface (messages counter) and show some pop-up. Otherwise do nothing (or turn inbox(x) to inbox(0) if your response states no new messages.

Also, a good solution would be to track user clicks on the new message (either in pop-up or in inbox) and send AJAX POST request with something like user_id=<user_id>&message=<message_id>status=read (I suggest you to make the status field, because maybe in future you will enable users to delete their messages without page refreshing/ mark read messages as unread).

On the server-side, the script (which AJAX request is sent to) should check the user (ways to identify user are up to you) and return all messages with status=<unread_marker> and user_id=<current_user_id>. Then wrap the answer to the format you want (the easiest one would be JSON, as I suggested) and respond. All other actions (i.e., updating message status) are done the same way.

Upvotes: 0

gastonfartek
gastonfartek

Reputation: 348

The only draw back is that not all browsers support WebSockets, and long-polling etc behave differently depending on the browser, your best bet would be to use socket.io, which works in every browser, it is mainly developed for nodejs, but there is also java implementations ...

Socket.io server : https://github.com/Ovea/Socket.IO-Java Socket.io client : https://github.com/Gottox/socket.io-java-client

official site : http://www.socket.io/ -> all the examples are based on nodejs

Upvotes: 1

Prisoner ZERO
Prisoner ZERO

Reputation: 14166

This is in C#...but you can easily convert it. Anyway, here are some classes I use for long-polling in C#. There are basically 6 classes (see below):

  • Controller: Processes actions required to create a valid response (db operations etc.)
  • Processor: Manages asynch communication with the web page (itself)
  • IAsynchProcessor: The service processes instances that implement this interface
  • Sevice: Processes request objects that implement IAsynchProcessor
  • Request: The IAsynchProcessor wrapper containing your response (object)
  • Response: Contains custom objects or fields

...I used this to create a chat application.

Upvotes: 0

Diodeus - James MacFarlane
Diodeus - James MacFarlane

Reputation: 114367

There are a few techniques available:

  • Polling (you talk about this)
  • Coment, a "long" poll that keeps the http connection open.
  • HTML5 Web Scokets

Polling is easy, the other two require a bit of experience in setting them up properly on the server. Keeping hundreds of simultaneous open connections can be a bit of a resource pig if you don't do it properly.

Upvotes: 0

Related Questions