peerjs icon indicating copy to clipboard operation
peerjs copied to clipboard

Outgoing signaling messages (Offer, Candidate) silently dropped when Peer ID is pre-set before WebSocket is open

Open codeCraft-Ritik opened this issue 1 month ago • 0 comments

Please, check for existing issues to avoid duplicates.

  • [x] No similar issues found.

What happened?

When a Peer is initialized with an explicit ID (e.g. const peer = new Peer("alice")), Peer immediately calls this._initialize("alice"), which executes this.socket.start("alice", token).

Inside Socket.start(), this._id = "alice" is assigned immediately, but the underlying WebSocket connection is still in the CONNECTING state (readyState === 0).

PeerJS documentation explicitly specifies:

"You may use the peer before this is emitted, but messages to the server will be queued. id is the brokering ID of the peer... You should not wait for this event before connecting to other peers if connection speed is important."

When the developer calls peer.connect("bob") or peer.call("bob", stream) right after instantiation:

  1. Negotiator constructs RTCPeerConnection, generates an Offer, and gathers early ICE candidates.
  2. The Offer message is forwarded to socket.send().
  3. Inside Socket.send(data) in lib/socket.ts:
    if (!this._id) {
        this._messagesQueue.push(data);
        return;
    }
    if (!this._wsOpen()) {
        return; // <--- The message is silently dropped!
    }
    
  4. Because this._id was already set in start(), if (!this._id) evaluates to false. The message bypasses the queue.
  5. It then reaches if (!this._wsOpen()) return;. Because the WebSocket is still connecting, this._wsOpen() is false.
  6. The function silently returns without pushing the message to this._messagesQueue.

When the WebSocket finally emits open, _sendQueuedMessages() drains an empty queue. The Offer and all early ICE candidates were dropped, causing the connection attempt to hang indefinitely until timing out.

How can we reproduce the issue?

  1. Initialize a peer with a specified ID:
    const peer = new Peer("alice", { host: "localhost", port: 9000, path: "/myapp" });
    
  2. Immediately call another peer without waiting for peer.on("open"):
    const conn = peer.connect("bob");
    conn.on("open", () => console.log("Connected!"));
    
  3. Observe WebSocket frames in developer tools: The WebSocket connection opens, but no OFFER message is ever transmitted to the server because it was dropped while the WebSocket was in CONNECTING state.

What do you expected to happen?

socket.send() should buffer any outgoing messages into this._messagesQueue whenever !this._wsOpen(), regardless of whether this._id is already assigned. Once the WebSocket opens, _sendQueuedMessages() will transmit the queued Offer and candidates.

Environment setup

Alice:

  • OS: Windows 11 / Ubuntu 22.04 / macOS Sonoma
  • Platform: Browser / Electron / Node.js
  • Browser: Chrome 122 / Firefox 123 / Safari 17.3

Bob:

  • OS: Ubuntu 22.04
  • Platform: Browser
  • Browser: Chrome 122

PeerJS version: 1.5.5

Is this a regression?

No, this bug exists in the current release (v1.5.5).

Anything else?

Notice the developer comment inside _sendQueuedMessages() in lib/socket.ts:113-114:

// Create copy of queue and clear it,
// because send method push the message back to queue if smth will go wrong

The original author intended send to queue messages if the socket is not ready, but checked !this._id instead of !this._wsOpen().

Proposed Fix:

--- a/lib/socket.ts
+++ b/lib/socket.ts
@@ -129,15 +129,9 @@ export class Socket extends EventEmitter {
-		// If we didn't get an ID yet, we can't yet send anything so we should queue
-		// up these messages.
-		if (!this._id) {
-			this._messagesQueue.push(data);
-			return;
-		}
-
 		if (!data.type) {
 			this.emit(SocketEventType.Error, "Invalid message");
 			return;
 		}

+		// Queue messages if WebSocket is not yet open
 		if (!this._wsOpen()) {
+			this._messagesQueue.push(data);
 			return;
 		}

 		const message = JSON.stringify(data);

 		this._socket!.send(message);

codeCraft-Ritik avatar Oct 08 '26 04:10 codeCraft-Ritik