Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 664
Dev2 Avatar asked Jan 31 '13 07:01

Dev2


2 Answers

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.

like image 68
rootkit Avatar answered Sep 18 '22 02:09

rootkit


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.

like image 27
Andres Olarte Avatar answered Sep 18 '22 02:09

Andres Olarte