{
socket.emit('request', /* … */); // emit an event to the socket
io.emit('broadcast', /* … */); // emit an event to all connected sockets
socket.on('reply', () => { /* … */ }); // listen to the event
});
There is a clear difference here between sending to a socket (socket.) and sending to the server (io.)
I have one use case in which I have some data in array and each array element I want to send to client one by one. So when first element is send via websocket I want to send second element from array only when we get response for first element from client and this should happen for all the array elements. How can this be done in websockets any suggestions or pointers?
Sounds pretty straightforward to me
Please can I ask a question 🙋
This isn't a classroom, don't ask to ask. Just ask the question and if someone can, they will help
If you do not have all the materials to learn javascript, or maybe you later get them after a long time, How do you learn on your own and still learn well, and how do you get to ever make money with all you have learned, am a javascript beginner, please advise me, I just can't code anymore I have a bad pc and I'm very frustrated, I don't want to stop code
Try freeCodeCamp.org, they have easy to follow courses that go from beginner levels to making you job-ready
I don't have a pc to code
I have 10 messages on server which I fetch from DB. Now i want to send message one by one. Lets say if I send 1st msg then I want to send second message only when we get response for 1st message. If we dont get response to 1st message then dont send second message. How will server know response is received for first message? How to keep track of that
Well you can wait for it
But how will you track if we have received response for 1st msg and then we send second similarly for 3,4,5th messages
loop does not work in case of socket messaging I read few blogs and they have mentioned there will be some deplay in messaging if we loop over individual messages. I have one more idea should I send entire data i.e 10 messages to client and then this looping logic will get handled on client side so I will send bulk data via socket i.e 10 messages in this case
That's not true
You can do bulking if that works for you, it would probably even be faster
By bulk I mean to say I have array of 10 messages with each message is object with some info so I will be sending entire array from server to client instead of sending single object one by one. Just wanted to know will this cause any performance issue? If I send data in bulk? Is there any limit on payload size?
No limits if you use highlevel libraries like socket.io
You can always click F12 instead of inspectin
Обсуждают сегодня