title: Screen sharing issue in WebRTC/nodejs with socket.io
body: <p>im working on a vidoe conferencing application and I cant seem to get the screen sharing working getting an error &quot;mediaTypeError: Cannot read properties of undefined (reading 'getSender')&quot;. The screensharing is able to start but nothing is being shared</p>
  <p>This is my server file.</p>
  <pre><code>const express = require(&quot;express&quot;);
  const app = express();
  const server = require(&quot;http&quot;).Server(app);
  const { v4: uuidv4 } = require(&quot;uuid&quot;);
  const io = require(&quot;socket.io&quot;)(server);
  // Peer
  
  const { ExpressPeerServer } = require(&quot;peer&quot;);
  const peerServer = ExpressPeerServer(server, {
    debug: true,
  });
  
  app.set(&quot;view engine&quot;, &quot;ejs&quot;);
  app.use(express.static(&quot;public&quot;));
  app.use(&quot;/peerjs&quot;, peerServer);
  
  app.get(&quot;/&quot;, (req, rsp) =&gt; {
    rsp.redirect(`/${uuidv4()}`);
  });
  
  app.get(&quot;/:room&quot;, (req, res) =&gt; {
    res.render(&quot;room&quot;, { roomId: req.params.room });
  });
  
  io.on(&quot;connection&quot;, (socket) =&gt; {
    socket.on(&quot;join-room&quot;, (roomId, userId) =&gt; {
      socket.join(roomId);
      socket.to(roomId).emit(&quot;user-connected&quot;, userId);
  
      socket.on(&quot;message&quot;, (message) =&gt; {
        io.to(roomId).emit(&quot;createMessage&quot;, message);
      });
    });
  });
  
  server.listen(process.env.PORT || 3030);
  </code></pre>
  <p>Codes for screensharing where there seem to be an issue with getSender</p>
  <pre><code>       share__Btn.addEventListener(&quot;click&quot;, (e) =&gt; {
          navigator.mediaDevices.getDisplayMedia({
              video: {
                  cursor: &quot;always&quot;
              },
              audio: {
                  echoCancellation: true,
                  noiseSuppression: true
              }
          }).then((stream) =&gt; {
              let videoTrack = stream.getVideoTracks()[0];
              let sender = currentPeer.getSender().find(function (s) {
                  return s.track.kind == videoTrack.kind
              })
              sender.replaceTrack(videoTrack)
          }).catch((err) =&gt; {
              console.log(&quot;unable to get display media&quot; + err)
          })
      })
   });
  
  peer.on(&quot;call&quot;, function (call) {
      getUserMedia(
          { video: true, audio: true },
          function (stream) {
              call.answer(stream); // Answer the call with stream.
              const video = document.createElement(&quot;video&quot;);
              call.on(&quot;stream&quot;, function (remoteStream) {
                  if (!peerList.includes(call.peer)) {
                      addVideoStream(video, remoteStream);
                      currentPeer = call.peerConnection
                      peerList.push(call.peer);
                  }
              });
          },
          function (err) {
              console.log(&quot;Failed to get local stream&quot;, err);
          }
      );
  });
  </code></pre>
  <p>Github link for the full codes: <a href="https://github.com/sucxh/simLearn" rel="nofollow noreferrer">https://github.com/sucxh/simLearn</a></p>