I am currently using socket.io to create a chat application in HTML and JS. I am able to communication between two computers easily sending written messages. My problem is that I am not able to figure out how to send real-time voice recorded in one computer to the other. I am using "navigator.getUserMedia" to record audio from the microphone and it works great, but I can not send it to through Socket.io to another computer.
I know that I can start recording, then stop and finally send the result, but it is not what I need, what I need is real-time streaming.
This is the code for the server:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var path = require('path');
var users = [];
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
socket.on('add user', function(user_id){
users.push(user_id);
});
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
socket.on('voice sent', function(msg){
io.emit('voice received', msg);
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
And this is client js:
$(function () {
var socket = io();
var user_id = Math.random();
socket.emit('add user', user_id);
$('form').submit(function(){
socket.emit('chat message', {"message": $('#m').val(), "user": user_id});
$('#m').val('');
return false;
});
socket.on('chat message', function(msg){
if(msg.user == user_id){
$('#messages').append($('<li class="mine">').text(msg.message));
}
else{
$('#messages').append($('<li>').text(msg.message));
}
});
socket.on('voice received', function(msg){
var audio = document.querySelector('audio');
audio.src = window.URL.createObjectURL(stream);
});
navigator.getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia;
if (navigator.getUserMedia) {
console.log('getUserMedia supported.');
navigator.getUserMedia ({audio: true}, successCallback, errorCallback);
} else {
console.log('getUserMedia not supported on your browser!');
}
function successCallback(stream){
socket.emit('voice sent', stream);
}
function errorCallback(error) {
console.error('An error occurred: [CODE ' + error.code + ']');
}
});
I hope somebody can help me. Thanks