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.