Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
120
rated 0 times [  124] [ 4]  / answers: 1 / hits: 8406  / 4 Years ago, fri, june 5, 2020, 12:00:00

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!


More From » mysql

 Answers
2

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 😊😊


[#3572] Tuesday, June 2, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
bryantc

Total Points: 455
Total Questions: 96
Total Answers: 110

Location: San Marino
Member since Thu, Jun 30, 2022
2 Years ago
bryantc questions
Fri, Aug 13, 21, 00:00, 3 Years ago
Tue, Mar 30, 21, 00:00, 3 Years ago
Wed, May 27, 20, 00:00, 4 Years ago
Wed, May 13, 20, 00:00, 4 Years ago
;