Is there a way to get notifications/chat/new messages indicator ?
-
Evening, I'm facing a question, is it possible with a socket event listener to get an update whenever something new happens. New unread messages, new notifications & new chat ?
The idea is that on my theme I've hidden this information and it's available when the menu is open but I want to be able to visually say hey there's new stuff open me.
Any idea on how I could do this ? I've thought of a SetInterval checking for notifications/messages/chat but performance wise it's absolutely not something cool.
I need this information cause I may change how my theme works now if not available.
Thanks for the further help/answers. -
socket.on('event:new_post', callback)
to catch a new post
socket.on('event:new_topic', callback)
to catch a new topic
socket.on('event:new_notification', callback)
to catch a new notification
socket.on('event:chats.receive', callback)
to catch a new chat message.Most of these are already used in core, but you can add your own listeners to add custom logic.
-
@baris It's working perfectly but for everybody is there a way to restrict the events to one concerned user ? I imagine that in the callback you have to test window.app.uid and maybe a socket.uid ?
I wanted to know if there's a way to check for the actual values, because this events gets fired only when something new is coming and the user is logged in. I want to be able to do the same callback if the user logs in and there's one of these that have some unread messages.
Thanks in advance. -
Not exactly sure what you are trying to do but each of these events send back some data, for example new_post sends the post object and you can compare the uid in that post object with app.uid to see if the post is made by the current user.
socket.on('event:new_post', function(data) { var post = data.posts[0]; if(parseInt(post.uid,10) !== app.uid) { console.log('someone else has posted!); } });
-
@baris Since my menu is hidden I want a visual indicator to say 'open me, there' s stuff inside' the indicator on the favicon gets updated in real time and when you login too. What I want to achieve is something similar but on the actual forum.
So chat notification events are linked to user while topic and posts are general. Thanks for the explanation I'll try that this evening.
The only missing point would be to be able to check for the values instead of the events to have it also when user just logs in/come back. I've thought of checking the apis of unread topics notifications and chat but maybe there' s a better way. -
@agarcia17 Yes I did but then I deleted it because I changed the way I displayed my menu after some feedback.
But here's what I've done//Check if there's unread messages var unread = setTimeout(checkUnread,500); //Update Unread messages on events socket.on('event:chats.receive', function(data) {toggleUnread(data)}); socket.on('event:new_notification', toggleUnread); socket.on('event:new_post', toggleUnread); socket.on('event:new_topic', toggleUnread);
function toggleUnread(data) { if (data) var receptor = data.message.touid; if (!$('#unread').length) { var unread = '<span id="unread" title="Vous avez des messages non lus" class="bounce-small infinite animated">'; if (receptor != undefined) { if (receptor == window.app.uid) $(unread).insertAfter('.header-top .menu'); } else { $(unread).insertAfter('.header-top .menu'); } } } function checkUnread() { $('#unread-count, #notif_dropdown i, #chat-count').each(function(){ var n = $(this).attr('data-content') if (n > 0) { toggleUnread(); return false; } }); }
I hope it can help you.