Skip to content

Server codebase and explanation

David F edited this page Nov 3, 2025 · 1 revision

In this section, we will talk about the server's codebase and try and give a starting point for development!

Note: the branch used for the server is 'beta' and the webclient should also be on the 'beta' branch.

Introduction/File Structure

In order to explain how our server works, we will start with the folder structure and index.ts file and instance.ts file

image

If we look at this image, all of our code is contained in the src folder. Let's focus on the index.ts file below.

image

When the server is instantiated (when you start it), the function PeerServer is called, which determines what port the server should use, and whether or not it should use https or http. This function then calls function ExpressPeerServer with the prior options mentioned. This function is shown below. Notice at the end the server calls createInstance() with a list of options. The next section will discuss createInstance()

image image

CreateInstance() aka instance.ts

CreateInstance() is located inside of instance.ts, this is where our server is created and starts to listen for client connections. We do this by creating a SocketIOServer. You can think of SocketIOServer as an object that the server uses for listening to, and sending messages to each client. You can see the creation of the SocketIO on line 61.

image

Now, the server needs to set up a few more pieces to enable the clients to videochat. These pieces are:

  • workers, roomList, and socketEvents.
  • workers: are used to create Routers (think of routers as stoplights, determining which clients can send and receive video and when they can do that)
  • roomList: A map <string, Room> holding a list of rooms. Note that the string key is the socket.room_id associated with that room. That is, each room gets a socket that all clients will connect to.
  • SocketEvents: These are specific request messages that the server is waiting to receive from each client. For example, in the below screenshot, a client uses the socket.io to send a messsage to the server asking to create a room:
image

As you can see, if the server receives this message, it will create a new room and add it to the roomList (to keep track of which rooms are made). You can see that there is a callback(room_id), this callback means that we send the room_id back to the client when completed.

Socket Events/messages and Why They Matter

As you can see, there are many socket.on() listeners. Why is this the case you may ask? The reason is because in order for videoChat to be completed, we must structurally complete certain tasks on both client side and server side.

Clone this wiki locally