kuttan pillai kuttan pillai - 23 days ago 13
Ajax Question

Broadcast a popup message to all online users Java,Jsp

i wish to add a functionality to my website that is to send a pop up message which i've typed, to all online users . i am able to get user id's of all online users. how could i send message in a jquery pop up to all users. Any one please give me an idea..

Answer

The solution to this requires 3 components:

First you need to make sure that whatever is typed is sent to the server. The easiest way is just via an Ajax call whenever you're done typing (or hit a button)

Secondly, you'll have to store the message(s) on the server including what users you've already distributed the message to. This can be just in memory on the server or in a database or file.

Lastly, each of the participating users will have to have a mechanism for retrieving the messages. The easiest way is just to have an Ajax call every 10 seconds for instance which asks the server if there are new messages. If there are they are returned and displayed in a popup.

The server can then mark the message as being sent to that user as well as keep a list of users that are active.

Note both the Ajax communications could be replaced with websocket connections if you want ultimate performance, but they are somewhat harder to set up, so I'd only recommend that if you have many users or many messages.

Comments