Outgoing signaling messages (Offer, Candidate) silently dropped when Peer ID is pre-set before WebSocket is open
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:
NegotiatorconstructsRTCPeerConnection, generates anOffer, and gathers early ICE candidates.- The
Offermessage is forwarded tosocket.send(). - Inside
Socket.send(data)inlib/socket.ts:if (!this._id) { this._messagesQueue.push(data); return; } if (!this._wsOpen()) { return; // <--- The message is silently dropped! } - Because
this._idwas already set instart(),if (!this._id)evaluates tofalse. The message bypasses the queue. - It then reaches
if (!this._wsOpen()) return;. Because the WebSocket is still connecting,this._wsOpen()isfalse. - 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?
- Initialize a peer with a specified ID:
const peer = new Peer("alice", { host: "localhost", port: 9000, path: "/myapp" }); - Immediately call another peer without waiting for
peer.on("open"):const conn = peer.connect("bob"); conn.on("open", () => console.log("Connected!")); - Observe WebSocket frames in developer tools:
The WebSocket connection opens, but no
OFFERmessage is ever transmitted to the server because it was dropped while the WebSocket was inCONNECTINGstate.
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);