I am trying to build a video chat web api using html and javascript. So far I found this code that accesses the microphones and webcam on the client's device and displays the video and audio on that client's screen. Here is that code:
<html>
<head>
<script>
navigator.getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia;
if (navigator.getUserMedia) {
navigator.getUserMedia({ audio: true, video: { width: 1280, height: 720 } },
function(stream) {
var video = document.querySelector('video');
video.src = window.URL.createObjectURL(stream);
video.onloadedmetadata = function(e) {
video.play();
};
},
function(err) {
console.log("The following error occurred: " + err.name);
}
);
} else {
console.log("getUserMedia not supported");
}
</script>
</head>
<body>
<video></video>
</body>
</html>
For the video chat web app to work the video and audio data will have to be sent back to the server where the server can process that data and send it to the other clients computer where both people can have a video chat. I have done some research and I believe that websockets might be my best bet for sending that video and audio data to the server and for the server to send to the other clients computer. I want to be able to make the websocket using both serverside and client side javascript. when I did some research I found some code that explains how to create a websocket connection and then how to send the server some data. here is the code I found for that:
var ws = new WebSocket("ws://localhost:9998/echo");
ws.onopen = function()
{
// Web Socket is connected, send data using send()
ws.send("Message to send");
alert("Message is sent...");
};
What I don't understand is how the server can receive that data using server side javascript. I also don't understand how the server side javascript is supposed to send that data to client side javascript on the other client computer via a websocket. I would extremely appreciate all ideas, source code, and links on how to send streaming video and audio data to a server, then for the server to receive the data using server side javascript, and then for the server to send that data to another clients computer. Similarly, I would like to know if websockets are a good way of completing this task, and if not, what ways would you suggest.
Thank you for your time!