Peer2Peer Connector Overview

Peer2Peer Connector is a WebSocket server designed to establish and manage real-time, low-latency connections between clients. It is the core component for enabling seamless peer-to-peer interactions and room management, making it an ideal solution for applications that require real-time data exchange and collaboration.

Key Features

1. Client Connection Management

2. Room Management

3. WebRTC Connection Establishment

4. Message Routing and Error Handling

Use Cases


Quick usuage

events supported

Notes
Example

WebSocket Connection

Initial Server Response

let webSocket = new WebSocket("wss://peer2peerconnector.shankarammai.com.np");

When a client successfully connects to the server, the server will send an initial message containing details about the connected client.

Message Structure example
{
    "type": "info",
    "event": "Client_Details",
    "data": {
        "id": "mqCCxD96EcRYmv5sop4YQL"
    },
    "timestamp": "2024-08-15T20:16:08.3438515+01:00",
    "message_id": "eed9d84c-2c88-4428-80eb-84aa35222aa6"
}
Fields Description

Notes

Connecting with another peer

To initiate a WebRTC connection with another peer, you can send a Connect request to the server. The server acts as an intermediary, facilitating the exchange of necessary signaling information between clients.

How to Establish a WebRTC Connection??

While the server itself doesn’t directly connect two clients via WebRTC, it plays a crucial role in enabling the connection. Clients must handle specific events such as Offer, Answer, Candidate, and Message —to successfully establish and manage the WebRTC connection. The proper handling of these messages by the client is essential for a successful peer-to-peer connection.

Example
{
  "event": "Connect",
  "to": "<clientId>",
  "data": {
    "sdp": "sdp",
    "candidate": "candidate"
  }
}
Fields Description

Error Handling

If a client tries to send a message to a peer that does not exist (i.e., the peer with the specified to client ID is not connected), the server will respond with an error message.

Error Message Structure
{
  "type": "error",
  "event": "Not_Found",
  "data": {
    "message": "Client with given 7KjNqESrhhTkEfXgFuaGWJ not found"
  },
  "timestamp": "2024-08-10T13:03:53.4821935+01:00",
  "message_id": "2007f6e7-34e5-4968-8470-f3dee63ce1b1"
}
Fields Description

Notes

Sending Other requests to another client

Clients can communicate with other peers by sending data using a specific message structure. The WebSocket server supports various message types, including offer, answer, candidate, and message. Below is the structure and an explanation of how to send and receive messages between peers.

To establish a WebRTC connection, clients must use the appropriate event. By sending the correct message such as an Offer, Answer, or Candidate the Peer2Peer Connector server will facilitate the connection between peers. It’s essential that clients handle these messages correctly to ensure a successful connection. The Peer2Peer Connector acts as the intermediary, guiding the connection process and ensuring smooth communication between peers.

Sending a message to another client

To send data to another peer, the client needs to send a message in the following format:

Example

To send a simple text message to another peer with the client ID DFPsXj9pygjNDcj2VyGzqQ, the client would send the following message:

{
  "event": "Message",
  "to": "DFPsXj9pygjNDcj2VyGzqQ",
  "data": "hellow world"
}
Fields Description

Received Message

When a client sends message to another peer using websocket it receives a message from server, the WebSocket server will send a message with the following structure:

Example

If a peer with the client ID 7TCqx3LCqPux3gQ9auwrH6 sends a message, the receiving client would receive:

{
  "data": "data",
  "from": "7TCqx3LCqPux3gQ9auwrH6",
  "event": "Message"
}
Fields Description

Error Handling

If a client tries to send a message to a peer that does not exist (i.e., the peer with the specified to client ID is not connected), the server will respond with an error message.

Error Message Structure
{
  "type": "error",
  "event": "Not_Found",
  "data": {
    "message": "Client with given 7KjNqESrhhTkEfXgFuaGWJ not found"
  },
  "timestamp": "2024-08-10T13:03:53.4821935+01:00",
  "message_id": "2007f6e7-34e5-4968-8470-f3dee63ce1b1"
}
Fields Description

Notes

Rooms

This Websocket supports events for room as well. Types that can be used are given below:

Fields Description
Example of creating room

If you want to create room.

{
  "event": "Create_Room",
  "data": {
    "room": "123456",
    "name": "my room name"
  }
}
Example of creating room response

Response when you send create room request. Clients are the list of clients in the room.

{
  "type": "info",
  "event": "Room_Created",
  "data": {
    "clients": ["WMYFTzZoX778PaiwjyZd59"],
    "name": "my room name",
    "roomId": "123456"
  },
  "timestamp": "2024-08-10T17:52:10.4816503+01:00",
  "message_id": "6e7ca893-26a3-420d-812d-98ea85a896cd"
}
Example of joining room

If you want to join room.

{
  "event": "Join_Room",
  "data": {
    "room": "123456",
  }
}
Example response of joining room
{
  "type": "update",
  "event": "Client_Added",
  "data": {
    "clients": [
      "UnVTfeUbHtbMH4cDoqKaCe",
      "L5RsWjtGXkHTG888LJoa8H"
    ],
    "name": "",
    "room": "hello"
  },
  "timestamp": "2024-08-10T19:19:31.6537518+01:00",
  "message_id": "330db08c-19d3-4e5a-a6bc-d6902f82272c"
}
Example of leaving a room

If you want to leave room.

{
  "event": "Leave_Room",
  "data": {
    "room": "123456",
  }
}
Example response of leaving room to another client
{
  "type": "update",
  "event": "Client_Removed",
  "data": {
    "clients": [
      "UnVTfeUbHtbMH4cDoqKaCe"
    ],
    "name": "",
    "room": "hello"
  },
  "timestamp": "2024-08-10T19:20:07.2508094+01:00",
  "message_id": "1a58b773-68fc-4b46-89d2-fec82e452877"
}
Example response of leaving room to the client requesting the leave
{
  "type": "info",
  "event": "Room_Left",
  "data": {
    "room": "hello"
  },
  "timestamp": "2024-08-10T19:20:07.2508094+01:00",
  "message_id": "1a58b773-68fc-4b46-89d2-fec82e452877"
}
Example of ending a room

If you want to end a room. Only creator can end the room.

{
  "event": "End_Room",
  "data": {
    "room": "123456",
  }
}
Example response of leaving room to all the clients, including the requester
{
  "type": "update",
  "event": "Room_Deleted",
  "data": {
    "clients": [
      "UnVTfeUbHtbMH4cDoqKaCe"
    ],
    "name": "",
    "room": "hello"
  },
  "timestamp": "2024-08-10T19:20:07.2508094+01:00",
  "message_id": "1a58b773-68fc-4b46-89d2-fec82e452877"
}
Notes