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.
Connect
: Used to initiate a connection with another client. This message should include data such as the SDP and candidate information.Offer
: Part of the WebRTC connection process, sent by a client to initiate a peer-to-peer connection.Answer
: Sent in response to an Offer
, completing the WebRTC connection setup.Candidate
: Contains ICE candidate information necessary for establishing the WebRTC connection.Message
: General-purpose message type for sending data between connected clients.Create_Room
: Used to create a new room. The message should include the room
and optionally the name
of the room, inside data
field.Join_Room
: Used to join a room. The message should include the room
inside data
field.Leave_Room
: Used to leave a room. The message should include the room
inside data
field.End_Room
: Used to end a room. The message should include the room
inside data
field.event
field.data
field.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.
{
"type": "info",
"event": "Client_Details",
"data": {
"id": "mqCCxD96EcRYmv5sop4YQL"
},
"timestamp": "2024-08-15T20:16:08.3438515+01:00",
"message_id": "eed9d84c-2c88-4428-80eb-84aa35222aa6"
}
info
.Client_Details
, indicating that the message contains information about the connected client.Client_Details
message correctly and stores the id
for future communication with the server.timestamp
is in UTC time and may need to be converted to the client’s local timezone if necessary.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.
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.
{
"event": "Connect",
"to": "<clientId>",
"data": {
"sdp": "sdp",
"candidate": "candidate"
}
}
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.
{
"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"
}
"error"
."Not_Found"
.to
field contains a valid client ID of an active peer.data
field contains sdp
and candidate
."error"
type messages in your client application to gracefully manage scenarios where the intended recipient is not available.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.
To send data to another peer, the client needs to send a message in the following format:
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"
}
"Offer"
, "Answer"
, "Candidate"
, "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:
If a peer with the client ID 7TCqx3LCqPux3gQ9auwrH6
sends a message, the receiving client would receive:
{
"data": "data",
"from": "7TCqx3LCqPux3gQ9auwrH6",
"event": "Message"
}
"Offer"
, "Answer"
, "Candidate"
, "Message"
.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.
{
"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"
}
"error"
."Not_Found"
.to
field contains a valid client ID of an active peer."error"
type messages in your client application to gracefully manage scenarios where the intended recipient is not available.This Websocket supports events for room as well. Types that can be used are given below:
Create_Room
Join_Room
Leave_Room
End_Room
"Create_room"
, "Join_room"
, "Leave_room"
, "End_room"
.If you want to create room.
{
"event": "Create_Room",
"data": {
"room": "123456",
"name": "my room name"
}
}
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"
}
If you want to join room.
{
"event": "Join_Room",
"data": {
"room": "123456",
}
}
{
"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"
}
If you want to leave room.
{
"event": "Leave_Room",
"data": {
"room": "123456",
}
}
{
"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"
}
{
"type": "info",
"event": "Room_Left",
"data": {
"room": "hello"
},
"timestamp": "2024-08-10T19:20:07.2508094+01:00",
"message_id": "1a58b773-68fc-4b46-89d2-fec82e452877"
}
If you want to end a room. Only creator can end the room.
{
"event": "End_Room",
"data": {
"room": "123456",
}
}
{
"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"
}