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 😊😊
socket.idis a temporal thing. As soon as the user hits refresh or goes to a different web page, thesocket.idwill change. More typically, you would keep track of that in memory, rather than in a mysql database. There is generally no need to persist asocket.idbecause 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).