0

I want to implement a notification system like facebook does for an Angular 7 app. I am currently using nodejs ad my backend and socket io for the socket connection. I understand the principle of sockets, events, observables... however, I can't wrap my head around how should I create a socket for each user to receive notifications.

I thought about creating a channel called 'notifications' and then subscribe all logged in users to that channel. When a user does anything that triggers an event that requires the app to create a notification I will broadcast the notification to all users and only display it if logged in user is inside the receivers array of the new notification object. HOWEVER I think this is a very bad approach as all users will be listening to this event and only a very small amount of them will display it. It seems like I am wasting a lot of resources with this approach. Is there a proper way to handle this or is my approach the one used by companies like Facebook and Instagram

  • i think you are talking about push notification or toaster notification .. there are already some library available (like primeng) for it. You need to implement a publisher-subscriber model for it. – Niladri May 05 '19 at 06:48
  • The users can optionally subscribe to this event , then only they will receive the notfication. – Niladri May 05 '19 at 06:49
  • Check one my answers is that helps you https://stackoverflow.com/questions/50371582/angular-6-passing-messages-via-service-to-from-component-to-message-component/50373010#50373010 – Niladri May 05 '19 at 06:50
  • @Niladri thanks for the reply, but I think your answer works for sharing information between components which are not related. What I'm trying to achieve is this: Let's say 1000 users follow user Niladri. When Niladri creates a new post I want to send a notification to those 1000 followers telling them "Niladri just posted something" is there a proper way to do this using socket.io? Thanks in advance – Rodrigo Villalobos May 05 '19 at 07:15
  • Sorry I never used nodejs as backend (I would use signalR for notifications) did you read this? https://blog.angular-university.io/angular-push-notifications/ – Geggi632 May 05 '19 at 08:40
  • @RodrigoVillalobos Did you find an answer to this? – joler-botol Apr 23 '20 at 05:12

0 Answers0