Monday 30 March 2020

video + audio real time chat using socket io in node js

​Hi guys,I am trying to do a real-time video + audio chat and I want to use client-server approach not peer to peer (Education purposes). Now I can stream only the video using code bellow. I want to use the same approach to send audio as well to my server using sockets a then emit to all connected sockets. Maybe socket.io-stream library can help if so how can I easily implement it into my code bellow? Or is there a better solution? Thanks.David// client code let socket = io.connect(window.location.origin); const video = document.querySelector('video'); const canvas = document.createElement('canvas'); const ctx = canvas.getContext('2d'); const img = document.querySelector('img'); const audiodata = document.querySelector("audio"); navigator.mediaDevices.getUserMedia({ video : true, audio : true }) .then(function(stream) { video.srcObject = stream; setInterval(function() { canvas.width = video.videoWidth; canvas.height = video.videoHeight; ctx.drawImage(video, 0, 0, canvas.width, canvas.height); const imageData = canvas.toDataURL("image/jpeg", 0.4); socket.emit('sendImage', { image : imageData }); }, 100); }).catch(error => console.error(error)); socket.on("getImage", function(data){ img.src = data.image; }) // server code let express = require("express"); let socket = require("socket.io"); const port = process.env.PORT || 3000 let app = express(); // static files app.use(express.static("public")); let server = app.listen(port, function() { console.debug(`listenning on port ${port}`); }) // socket setup let io = socket(server); io.on("connection", function(socket){ console.debug(`made socket connection with id ${socket.id}`); socket.on("sendImage", function(data){ socket.broadcast.emit("getImage", data); }); });

Submitted March 30, 2020 at 01:12PM by prois99

No comments:

Post a Comment