0

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

Description:

  • Letter A is simple, I am just describing.

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:

  • Lots of ideas come to my mind like doing some servlet in java that returns the message count from db and this is printed by a jquery post. But how can I achieve this exactly at the momment that the message arrives or when the user logs into the system after a long time of inactivity.

  • Also a setInterval from javascript came to my mind but suppose that I program the interval to check every 5 seconds for a new message, what happens if the user sent the it and took him just 2 seconds. I think a delay happens.

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

Thanks in advance.

Oscar Jara
  • 14,129
  • 10
  • 62
  • 94
  • The technology you are looking looking for is called "Server Push". Here is a wikipedia page: http://en.wikipedia.org/wiki/Push_technology – DwB Mar 23 '12 at 17:50

4 Answers4

1

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

gastonfartek
  • 348
  • 1
  • 10
0

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.

Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
  • Do you know any example or demo about polling for this case? I will read about comet but do you know if all this could work with Tomcat 7? Also HTML5 is not a good option because its not supported in all browsers. I found something called "socked.io" but I don't know how to use it, also i think this requires nodejs and everything goes complicated :-) – Oscar Jara Mar 23 '12 at 17:55
  • Polling is simple an AJAX call: "My last message was #30213. Are there any new messages for me?". The rest is a simple DB query. – Diodeus - James MacFarlane Mar 23 '12 at 17:58
  • One short note: I recommend to extend the DB table with a status flag in order to track if the message has been read. –  Mar 23 '12 at 17:51
0

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.

Community
  • 1
  • 1
Prisoner ZERO
  • 13,848
  • 21
  • 92
  • 137
0

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.

madfriend
  • 2,400
  • 1
  • 20
  • 26