Skip to content
Advertisement

Screen sharing issue in WebRTC/nodejs with socket.io

im working on a vidoe conferencing application and I cant seem to get the screen sharing working getting an error “mediaTypeError: Cannot read properties of undefined (reading ‘getSender’)”. The screensharing is able to start but nothing is being shared

This is my server file.

const express = require("express");
const app = express();
const server = require("http").Server(app);
const { v4: uuidv4 } = require("uuid");
const io = require("socket.io")(server);
// Peer

const { ExpressPeerServer } = require("peer");
const peerServer = ExpressPeerServer(server, {
  debug: true,
});

app.set("view engine", "ejs");
app.use(express.static("public"));
app.use("/peerjs", peerServer);

app.get("/", (req, rsp) => {
  rsp.redirect(`/${uuidv4()}`);
});

app.get("/:room", (req, res) => {
  res.render("room", { roomId: req.params.room });
});

io.on("connection", (socket) => {
  socket.on("join-room", (roomId, userId) => {
    socket.join(roomId);
    socket.to(roomId).emit("user-connected", userId);

    socket.on("message", (message) => {
      io.to(roomId).emit("createMessage", message);
    });
  });
});

server.listen(process.env.PORT || 3030);

Codes for screensharing where there seem to be an issue with getSender

       share__Btn.addEventListener("click", (e) => {
        navigator.mediaDevices.getDisplayMedia({
            video: {
                cursor: "always"
            },
            audio: {
                echoCancellation: true,
                noiseSuppression: true
            }
        }).then((stream) => {
            let videoTrack = stream.getVideoTracks()[0];
            let sender = currentPeer.getSender().find(function (s) {
                return s.track.kind == videoTrack.kind
            })
            sender.replaceTrack(videoTrack)
        }).catch((err) => {
            console.log("unable to get display media" + err)
        })
    })
 });

peer.on("call", function (call) {
    getUserMedia(
        { video: true, audio: true },
        function (stream) {
            call.answer(stream); // Answer the call with stream.
            const video = document.createElement("video");
            call.on("stream", function (remoteStream) {
                if (!peerList.includes(call.peer)) {
                    addVideoStream(video, remoteStream);
                    currentPeer = call.peerConnection
                    peerList.push(call.peer);
                }
            });
        },
        function (err) {
            console.log("Failed to get local stream", err);
        }
    );
});

Github link for the full codes: https://github.com/sucxh/simLearn

Advertisement

Answer

I’m assuming that currentPeer is an RTCPeerConnection as documented here: https://peerjs.com/docs.html#dataconnection-peerconnection. In that case it’s a simple typo. The method is called getSenders() and not getSender(). Adding the missing “s” should make the error go away.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement