1

I am trying to create a notification system for my Events Manager Website.

  1. Whenever a user is logged in and does something (for example if he creates an event), the notification that he has created an event should be sent to the other users .The notifications should be available in the '/notification/:username' page which is authenticated (where the username is unique for each user).

  2. Also if the user creates a private event ,the notification must be sent to the concerned users.

I am using Nodejs(Express),socket.io,Vanilla javascript,mysql.For this if I am correct, I need to store the clientid(in my case,it is username) and socketid in database(mysql) as key value pairs .But I am so confused on how to proceed ? I don't know how to get the socketid for different users and store in database. I have no clue how to proceed. It would be really great if someone explain me what should I do ,what are the things I need to tackle the problem,etc. Thank you!

4
  • socket.id is a temporal thing. As soon as the user hits refresh or goes to a different web page, the socket.id will change. More typically, you would keep track of that in memory, rather than in a mysql database. There is generally no need to persist a socket.id because it can never last longer than your server. A clustered setup might store the id centrally so any server in the cluster can get it (that is often done with a memory-based store like redis). Commented Jun 5, 2020 at 5:24
  • Ok so I am not using redis .Is there anyway for storing socketids for particular users ? And also if there is some other approach to solve this other than using socket.id , you can tell me . Thanks Commented Jun 5, 2020 at 5:29
  • There is a problem, you need to be connected to listen to the notifications, and receive them, if the socket is disconnected you can't receive notifications, so what you are looking for isn't socket.io or sockets, you are looking for push notifications (investigate about that), is a service that you can use to handle notifications also when the user is not connected, (like whatsapp notifications), an example of push notifications service is onesignal, i use onesignal to work with ionic for android, but i think can be used for webpages also. Commented Jun 5, 2020 at 5:42
  • Can you tell me about resources where I can learn about push notifications? Commented Jun 5, 2020 at 5:45

1 Answer 1

6

I'm gonna try to explain some various ways to you can proceed.

So from my understanding you already have a users system attached to your mysql and such so you know the users info right?

So I'm an auth user on ur system and i can create an event, when it's public it need's to be sended everyone right?

what you can use for this is following:

io.emit('event-created', data);` // so this part from server is gonna send everyone // your frontend part should recieve this event. socket.on("event-created", function(data) { // this is gonna be a public event anyway so everyone can see it // so here you have your newly created event's data so use it as needed =) }); 

So the second part is a bit more complicated, it needs to be private right?

Here what you can do, you told us that u'r username's are uniq right? so everyone can join its own room named as their username so in your connection you might do something like this:

io.on("connection", (socket) => { // I'm assuming that you have a username here socket.join('uniq-username'); }); 

What we have here that lets say for me, if you want to send an event only named halilcakar this is my username and u already added me to my room. What you can do is

io.in('halilcakar').emit('some-event', { ...withSomeData });

When u use this in somewhere in your code, and if i'm online this event is gonna be only seeable by me.

So What i'm trying to tell again assuming that while creating private event we need to select which user is included with this am i right?

So lets right in this code.

// this part is already in your connection // create a private event socket.on('private-event-created', data => { // here the data coming from frontend // while sending this down just make sure data // has a property called users as usernames and array maybe? // since you created this event what you can do is send an emit to // each user like following: data.users.forEach(username => io.in(username).emit('private-event', data)); // So basicly we are sending this event to // every user selected by the creator of this private event // again the part giving this ability is | .in(username) | this part }); 

After everything, you need to listen for this private-event event from socket.io

// so on frontend socket.on('private-event', function(data) { // here do the private events objectives. // and the good part that even me(single user) // I'll know who has this private-event by // data.users }); 

I'm hoping this will gave you more ideas :)) Please feel free to ask 😊😊

Sign up to request clarification or add additional context in comments.

3 Comments

I've a question, but is in frontend, if i can connect to user channel, the code has been: socket.join('username').on('event')
Hi David, basicly no, frondend doesn't have a join method on it. you just listen for events from frontend and backend decides to send the events or not :)
I'm new so i'm interested

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.