files/en-us/web/api/rtcdatachannel/readystate/index.md
{{APIRef("WebRTC")}}
The read-only RTCDataChannel property readyState returns a string which indicates the state of the data channel's underlying data connection.
A string indicating the current state of the underlying data transport, which is one of the following values:
connecting
open
closing
closed state.closed
const dataChannel = peerConnection.createDataChannel("File Transfer");
const sendQueue = [];
function sendMessage(msg) {
switch (dataChannel.readyState) {
case "connecting":
console.log(`Connection not open; queueing: ${msg}`);
sendQueue.push(msg);
break;
case "open":
sendQueue.forEach((msg) => dataChannel.send(msg));
break;
case "closing":
console.log(`Attempted to send message while closing: ${msg}`);
break;
case "closed":
console.log("Error! Attempt to send while connection closed.");
break;
}
}
{{Specifications}}
{{Compat}}