简体   繁体   中英

Send a message from Server to All Clients

I know I don't have any code to show you guys, I am stuck at some point and I dont know where to start. I hope someone will help me.

I am developing a Spring MVC application, and I need to send a message to all active session users to execute a script which is available to all the clients as it is defined in a js file and included for every user.

I have looked around and found some frameworks which offers these type of functionalities like Atmosphere but I don't think it should be used in my situation as it is a big framework and the functionality required is very little. I have also gone thorough WebSockets but I cant find anything which would help me in invoking the script on client side for all the clients.

If someone can help me go to a right path or direct me to a similar example. I will be grateful

****Update**** I can also use polling if there is way that: if the controller gets a request the session should be considered idle during that, for instance, there is controller which is called every 5 minutes and in the session out time is 30 minutes. The session won't expire in this time if the controller used for polling is called every 5 minutes, I need to exclude the particular controller from calculating the idle time

No Polling Solution: From what I gather, you need a Remote Procedure Call mechanism. I would go with https://github.com/eriksank/rpc-websocket . The general idea:

  1. Your clients register themselves to your server process as "consumers".
  2. When a message is ready, your server then goes through every registered "consumer" and sends the message which rpc-websocket can handle .

Polling Solution: Here is a general idea, works if you have registered, logged on users.

  1. Have a database table that stores messages, lets call it " messages ".
  2. Have a database table that keeps track of messages and users, lets call it " message_tracker ". if a user has seen a message, there will be a row in this table for the messageId and UserID.
  3. Have a javascript script poll a server url for new messages. What is a new message can be decided based on the database tables above.
  4. If a new message is found, process it and then call another server url which inserts into the message_tracker database table

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM