Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

372
Views
Notification system using socket.io

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!

over 4 years ago ยท Santiago Trujillo
1 answers
Answer question

0

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 ๐Ÿ˜Š๐Ÿ˜Š

over 4 years ago ยท Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!