0

I have a problem with some Socket.IO (ReactJS+NodeJS) application. I have application which synchronize playing YouTube videos (something like Watch2Gether) and I have Playlist feature - after end of video (for example) 5 clients sends to NodeJS action END_OF_VIDEO and Node should send new Video ID to all clients. And here is the problem: how should I synchronize this to send ONLY 1 response with next video from playlist, but not 5 responses which cause skipping 5 videos in playlist (because of 5 emmited END_OF_VIDEO actions from client)?

John Doe
  • 3
  • 4

1 Answers1

0

Server:

io.on('connection', function (socket) {
  socket.on('end of video', function (video_number) {
    if(video_number){
      // Some logic to find link_to_next_video using video_number
    }
    socket.emit('next video', link_to_next_video);
  });
});

Client:

socket.emit('end of video', current_video_number);

// Receiving 'next video' event
socket.on('next video', function(link){
  // code to play video of received link
});

Here socket.emit will send 'next video' event to the client who had sent 'end of video' event to server.

So, no matter how many clients send the same 'end of video' event to server. Server will respond to all the clients exactly one time with 'next video' event.

You would like to send some data when sending 'end of video' event from client to recognize which video is currently played by the client and then decide according to that what video link should be sent with 'next video' event to that client.

Krunal-Gadhiya
  • 351
  • 2
  • 8
  • @theNeddle I understand your suggestion but it doesn't solve my problem. I'll try to explain it better. I have Playlist (list of NEXT videos) for example: [vid1, vid2, vid3 ...] and when some client sends END_OF_VIDEO event then server should look for next video in playlist (vid1 here) send it to client and then REMOVE it from playlist and send updated playlist [vid2, vid3 ...]. Now when server will receive END_OF_VIDEO from second client, it will respond with vid2, next with vid3 etc... 2nd think is that I want to send this information to whole room in the same time for best synchronization. – John Doe Mar 14 '18 at 22:37
  • @JohnDoe I'm not able to understand exactly what you want to do. If you can share some code I might be able to help. Also [this](https://stackoverflow.com/questions/35385609/random-chat-with-2-users-at-a-time-socket-io/35387759#35387759) question might help you. – Krunal-Gadhiya Mar 15 '18 at 10:35