I've tried to implement the basic notification system for a basic social network with p:poll
on view layer and a simple NotificationService
class which gets the new notifications from DB and refreshes the notifications list of NotificationBean
which is viewscoped for each user. Process flow similar to this:
-Poll calls NotificationBean.getNewNotifications for example every 15 sec.
--getNewNotifications calls NotificationService and DAO methods
---notificationList of user is refreshed
----DataTable on view layer shows new notifications
But the concern of p:poll
is about it's performance because it sends a query at every interval expiration.
PrimeFaces has PrimePush which based on Atmosphere Framework, it opens web-sockets and seems like more suitable for creating notifications system.
But I don't know which components and which properties of them should be used. It has p:socket
component with channel
property. Should I use usernames as a channel
values? Below code coming from PrimeFaces showcase and summarizes the last sentences:
<p:socket onMessage="handleMessage" channel="/notifications" />
As far as I understood from this showcase example this p:socket
listens notifications
channel. And pusher code snippet is:
PushContext pushContext = PushContextFactory.getDefault().getPushContext();
pushContext.push("/notifications", new FacesMessage(summary, detail));
But this will notify all user pages, I need a pusher which notifies specific user. Let say there are 2 users and assume that User1 adds User2 as a friend. There must be sth. like that:
pushContext.push("User2/notifications", new FacesMessage("friendship request", "from User1"));
But I am not sure this is the correct usage for this kind of functional requirement or not. Considering scalability of the app there can be expensive cost of opening so many channels per a process.
Thanks for helping.
PrimeFaces push supports one or more channels to push. To be able to create private channels for specific reasons; for example per user like in your case, you can create more than one channels. I had used unique ids for this purpose.
Basically, I've implemented a managed bean which is application scoped that handles user channel matching which should be considered. You can maintain it in different ways.
Then inject this bean in your backing bean which sends notifications.
You should give the channel value to p:socket. Here is the kickoff example of the page;
For scalability issues, you should maintain the active or inactive channels. You can remove the one which is not in session or inactive for some time. Remove channels by using @PreDestroy annotation when beans are destroying. There is one channel for one user session in my solution.
My suggestion is; do not use usernames explicitly on the pages. It is not good for security reasons.