ondatachannel event never fires – WebRTC

Offer Side

let pc = new RTCPeerConnection({});
let dc = pc.createDataChannel("msg");
dc.onopen = function (e) {
  console.log("Channel Opened", e);
};
dc.onclose = function (e) {
  console.log("Channel closed");
};

let offer = await pc.createOffer();
await pc.setLocalDescription(offer);

socket.emit("offer", JSON.stringify(pc.localDescription));

socket.on("answer", function (sdp) {
  pc.setRemoteDescription(JSON.parse(sdp));
});

Answer Side

let pc = new RTCPeerConnection({});
pc.ondatachannel = function (e) {
  console.log(e);
};
socket.on("offer", async function (sdp) {
  await pc.setRemoteDescription(JSON.parse(sdp));
  let ans = await pc.createAnswer();
  pc.setLocalDescription(ans);
  socket.emit("answer", JSON.stringify(ans));
});

Ideally ondatachannel event should be fired.

Can someone tell me that why this is not working?

Thanks in advance 🙂

Source code

https://github.com/ats1999/RTSP-webRTC/tree/main/webRTC/data-channel

Do npm i to install the server side dependencies.