Skip to content

Socket.io

George Mcerlean edited this page May 24, 2020 · 1 revision

Web Sockets

In order to allow for users to communicate in real time and watch videos synchronously. We use the Socket.io library. In order to allow for multiple rooms to be setup we used the Socket.io rooms functionality which allows the sockets to send messages to a select few sockets. To join a room you use socket.join({roomID}) and to leave a room you use socket.leave({roomID})

Socket.io Backend Schema

socket.on('join', ...)

This is received when a user joins a room. It then emits a message to all users within the room informing them they have joined.

socket.on('pause' ...)

This is received when a user presses the pause button in a room. This function checks if the user is the host, if they are, it distributes a pause to all users.

socket.on('play', ...)

This is received when a presses the play button in a room. This function checks if the user is the host, if they are, it distributes a play to all users.

socket.on('message', ...)

This is received when a user posts a message in the chat. This function then emits the message to all users in the room.

socket.on('change', ...)

This is received when the host changes the video. This functions distributes the video update to all users.

socket.on('leaveRoom', ...)

This is received when a user leaves the room. This method detects if the user was the host. If they were the host it distributes a host left message and the room is dropped, otherwise a user left message is sent and the user is removed from the viewers model.

socket.on('disconnect', ...)

This is received when a users socket disconnects. This methods then checks if the user was in a room it distributes a userleft message and then removes them from the viewers model.

Socket.io Frontend Schema

socket.on('userJoinedRoom', ...)

This is received when another user leaves the room. This method then updates the user list with the new user.

socket.on('newMessage', ...)

This is received when another user posts a message in the chat. This updates the chat to include the new message.

socket.on('playVideo', ...)

This is received when the host presses play. This method then starts the users video playing at the given time

socket.on('pauseVideo', ...)

This is received when the host presses pause. This method then stops the users video at the given time

socket.on('changeVideo', ...)

This is received when the host changes the video. This method then changes the users video to the updated video provided.

socket.on('userLeft', ...)

This is received when a user leaves the room. This method then removes the user from the user list.

socket.on('hostLeft', ...)

This is received when the host leaves the room. This method then prompts this user to redirect to the homepage.

Clone this wiki locally