How to send notification to all logged in users
I've got a Spring MVC, Spring Thymeleaf, Spring Security and Hibernate on MySQL application. My task is to notify to all currently logged in users if there is new data persisted for the page they are viewing so that the users can refresh and get a new set of data.
I was guessing that I could store all the logged in users in an ApplicationScope session and then notify them somehow. But I've never done this before and not sure how safe it would be.
Can anyone suggest any idea on how this could be done ?
You need push/pull architecture (to initiate sending messages from the server side). None of the Spring frameworks you use can do that. Check out Atmosphere https://github.com/Atmosphere/atmosphere
Then whenever new data is persisted (your DAO layer can trigger this) you can just push new message via Atmosphere servlet to all subscribed users. Atmosphere uses "channels" so you can use predefined channel name for the page users are on, and all users will get the message. You can also secure Atmosphere servlet with Spring security so unauthenticated users wont be able to get notifications.
Based on the above comments, you could implement a pull architecture with something like this:
Store all events in a central location, accessible by all sessions (Singleton, Database, etc.) Make sure that there's a sequential ID.
Store the latest event ID retrieved on the session. As often as needed, query for events with IDs greater than the latest ID stored in the session.
Hope this helps.
链接地址: http://www.djcxy.com/p/68886.html上一篇: 为什么'for window'中的for循环初始化会导致语法错误?
下一篇: 如何向所有登录用户发送通知