Reputation: 14187
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:
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.
Upvotes: 0
Views: 1829
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
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
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):
...I used this to create a chat application.
Upvotes: 0
Reputation: 114367
There are a few techniques available:
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