2

I am using angular-websocket to open a WebSocket, The application is waiting for a message and then displays a web notification.

I noticed that when there is more than 1 tab open there are many connections and then every tab will receive a message and notifications will be displayed 1 X TABS_NUM when it should be displayed once.

I think that to make it work properly I need to open 1 WebSocket for the whole app.

Any ideas? What should I do in that case?

Mor Paz
  • 2,088
  • 2
  • 20
  • 38
Oz Bar-Shalom
  • 1,747
  • 1
  • 18
  • 33
  • Each window is a separate instance of your client side app. Really not clear what behavior you are looking for – charlietfl May 31 '17 at 12:36
  • I have a websocket that send notifications. But It is not right to send 2 of the same notification for the client if he have 2 open tabs... what should I do in that case – Oz Bar-Shalom May 31 '17 at 12:38
  • can't you just emit a message when it is read in one tab and other tab then knows about it? – charlietfl May 31 '17 at 12:42
  • @charlietfl can you please explain more ? – Oz Bar-Shalom May 31 '17 at 12:44
  • Are you using [Notifications API](https://developer.mozilla.org/en-US/docs/Web/API/Notifications_API/Using_the_Notifications_API#Receiving_notification_of_clicks_on_app_notifications)? If so can `tag` notifications to avoid duplicates also. If not ... show more details – charlietfl May 31 '17 at 13:03

1 Answers1

1

Using tag solved my issue:

It is usually undesirable for a user to receive a lot of notifications in a short space of time — for example, what if a messenger application notified a user for each incoming message, and they were being sent a lot? To avoid spamming the user with too many notifications, it's possible to modify the pending notifications queue, replacing single or multiple pending notifications with a new one.

To do this, it's possible to add a tag to any new notification. If a notification already has the same tag and has not been displayed yet, the new notification replaces that previous notification. If the notification with the same tag has already been displayed, the previous notification is closed and the new one is displayed.

new Notification(notificationMessage.title, {tag: notificationMessage._id});
Oz Bar-Shalom
  • 1,747
  • 1
  • 18
  • 33