How to use socket.io-stream for live video/audio streaming

Viewed 8732

I am trying to make a real time P2P video chat application.

I found out that using socket.io-stream library would help. But I cannot figure out how to implement this library.

My video player is working fine at client side though. I just want to transfer this stream to other connected clients using socket.io.

My server code is given below

let app = require("express")();
let ss = require("socket.io-stream");
let http = require("http").createServer(app);
const routes = require("./routes");
let io = require("socket.io")(http);
routes(app, __dirname);
http.listen(3000, () => {
    console.log("app listening on port ", 3000);
});

io.sockets.on("connection", (socket) => {
    socket.on("joinRoom", (obj) => {
        let { name, stream } = obj;
        console.log(name, stream);
    });
});

and my client side script is given below

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Video Calling</title>
</head>

<body>
    <video autoplay="true" id="video"></video>
    <script src="/socket.io/socket.io.js"></script>
    <script>
        const socket = io();
        let name = prompt("Enter the room name");

        function wrongname() {
            name = prompt("Seriously? That's not a name! enter again!");
            validator();
        }

        function validator() {
            if (!name.match(/^[0-9a-z]+$/)) {
                wrongname();
            } else {
                return true;
            }
        }
        validator();
        let video = document.getElementById('video');
        if (navigator.mediaDevices.getUserMedia) {
            navigator.mediaDevices.getUserMedia({
                    video: true,
                    audio: true
                })
                .then((stream) => {
                    video.srcObject = stream;
                    console.log(stream);
                    socket.emit("joinRoom", {
                        name: name,
                        stream: stream
                    });
                })
                .catch((error) => {
                    alert("You choose to turn off your video and audio");
                    location.replace('/');
                })
        }
    </script>
</body>

</html>

I repeat, my problem is to stream video through socket.io using socket.io-stream(if there is any other alternative it is fine)

My code with GitHub link up to this point is here

I hope my query is easy to understand.

1 Answers

I am trying to make a real time P2P video chat application.

This is the wrong technology. You need to use WebRTC to ensure realtime streams, and to make peer-to-peer connections.

I found out that using socket.io-stream library would help.

No, it won't. The term "stream" is overloaded here... the NPM module you're referring to is for the Stream API for piping data. The "stream" you get from the Media Devices API is a MediaStream... completely unrelated to the Streams API.

Related