socket.io-stream icon indicating copy to clipboard operation
socket.io-stream copied to clipboard

Is it possible to Stream Audio form the Server to the client ?

Open daslicht opened this issue 9 years ago • 27 comments

Is it possible to Stream Audio form the Server to the client ? Is there any example please ?

daslicht avatar Mar 07 '16 11:03 daslicht

Yes it is possible

Server

io.on('connection', function (socket) {
  socket.on('client-stream-request', function (data) {
    var stream = ss.createStream();
    var filename = __dirname + '/downloads/' + <YOURSONG.MP3>;
    ss(socket).emit('audio-stream', stream, { name: filename });
    fs.createReadStream(filename).pipe(stream);
  });
});

Client

var audio = document.getElementById('player');
ss(socket).on('audio-stream', function(stream, data) {
    parts = [];
    stream.on('data', function(chunk){
        parts.push(chunk);
    });
    stream.on('end', function () {
        audio.src = (window.URL || window.webkitURL).createObjectURL(new Blob(parts));
        audio.play();
    });
});

duncanhoggan avatar Oct 28 '16 09:10 duncanhoggan

AWESOME, i will try it soon ! <3 THANK YOU VERY MUCH!

daslicht avatar Oct 28 '16 09:10 daslicht

Is it also possible to send additional data with it like Metadata and is it even possible to send it to a specific point of time ?

daslicht avatar Oct 28 '16 09:10 daslicht

You can emit it as a separate event or include it in the data object that is sent with the stream

ss(socket).on('audio-stream', function(stream, data) {}

duncanhoggan avatar Oct 28 '16 10:10 duncanhoggan

ok , but what about time sensitive data ? Lets say I like to push metadata once the Playback has reach 1Minute, 30 Seconds of the source ? Is at hat possible at all ?

daslicht avatar Oct 28 '16 12:10 daslicht

Hi, Could you provide the full code?

NecroKills avatar Apr 18 '17 12:04 NecroKills

With this html part its working:

    <audio id="player" controls>
    <source src="" type="audio/mpeg">
        Your browser does not support the audio element.
    </audio>

zoutepopcorn avatar Jun 12 '17 05:06 zoutepopcorn

Example over here :D https://github.com/zoutepopcorn/audio_socket/tree/master

zoutepopcorn avatar Jun 12 '17 11:06 zoutepopcorn

is there a way to do it in real time?

schnells avatar Oct 11 '18 20:10 schnells

There is a way to stream microphone in a real time, but I have small problem on my implementation..

Update: I just uploaded this library, but it still need some improvement https://github.com/ScarletsFiction/SFMediaStream

StefansArya avatar Nov 25 '18 05:11 StefansArya

@StefansArya Can I use that library to stream binary blobs (sent as chunks of 3-seconds) ? Finding it very difficult to implement

avin3sh avatar Dec 29 '18 17:12 avin3sh

Hmm, I think you could only send the buffer data (without media header). Because that's what I can get after calling mediaRecorder.requestData().

To stream chunks of 3 seconds you need to change the latency to 3000ms. I have an example on the repository that default to 100ms, maybe you can use that for experimenting.

And don't forget to set the similar latency to the streamer too.

StefansArya avatar Dec 30 '18 05:12 StefansArya

Yes it is possible

Server

io.on('connection', function (socket) {
  socket.on('client-stream-request', function (data) {
    var stream = ss.createStream();
    var filename = __dirname + '/downloads/' + <YOURSONG.MP3>;
    ss(socket).emit('audio-stream', stream, { name: filename });
    fs.createReadStream(filename).pipe(stream);
  });
});

@duncanhoggan , is it a good idea use sockets to build a streaming server to work as a streaming service on demand?

ithustle avatar Mar 30 '19 17:03 ithustle

that doesn't take bandwidth throttling into account no ? I just push as fast as possible no ?

daslicht avatar Mar 31 '19 09:03 daslicht

Yes it is possible Server

io.on('connection', function (socket) {
  socket.on('client-stream-request', function (data) {
    var stream = ss.createStream();
    var filename = __dirname + '/downloads/' + <YOURSONG.MP3>;
    ss(socket).emit('audio-stream', stream, { name: filename });
    fs.createReadStream(filename).pipe(stream);
  });
});

@duncanhoggan , is it a good idea use sockets to build a streaming server to work as a streaming service on demand?

ss is not defined

yusuf987 avatar Jul 13 '19 08:07 yusuf987

Yes it is possible

Server

io.on('connection', function (socket) {
  socket.on('client-stream-request', function (data) {
    var stream = ss.createStream();
    var filename = __dirname + '/downloads/' + <YOURSONG.MP3>;
    ss(socket).emit('audio-stream', stream, { name: filename });
    fs.createReadStream(filename).pipe(stream);
  });
});

Client

var audio = document.getElementById('player');
ss(socket).on('audio-stream', function(stream, data) {
    parts = [];
    stream.on('data', function(chunk){
        parts.push(chunk);
    });
    stream.on('end', function () {
        audio.src = (window.URL || window.webkitURL).createObjectURL(new Blob(parts));
        audio.play();
    });
});

what is ss here ????

yusuf987 avatar Jul 13 '19 08:07 yusuf987

what is ss here ????

see: https://github.com/nkzawa/socket.io-stream

daslicht avatar Jul 13 '19 09:07 daslicht

The solution @duncanhoggan provided is working. But what it do is it firstly read the whole file then after that it plays. But What I want is, I want to stream the file into chunks. There can be the delay for max 4-5 seconds.

Can anyone please provide the solution for this?

rajatlnweb avatar Oct 04 '19 13:10 rajatlnweb

@rajatlnweb if you send it in chunks of 4-5 seconds, it will play it that way too.

avin3sh avatar Oct 04 '19 13:10 avin3sh

@avin3sh , Can you please give me the code example as I am not able to find it out anywhere?

rajatlnweb avatar Oct 04 '19 13:10 rajatlnweb

Look above: https://github.com/nkzawa/socket.io-stream/issues/73#issuecomment-307763328

zoutepopcorn avatar Oct 07 '19 08:10 zoutepopcorn

@zoutepopcorn , I checked your code and it's not related to chunk based audio play at all.

rajatlnweb avatar Oct 07 '19 08:10 rajatlnweb

Yes it is possible

Server

io.on('connection', function (socket) {
  socket.on('client-stream-request', function (data) {
    var stream = ss.createStream();
    var filename = __dirname + '/downloads/' + <YOURSONG.MP3>;
    ss(socket).emit('audio-stream', stream, { name: filename });
    fs.createReadStream(filename).pipe(stream);
  });
});

Client

var audio = document.getElementById('player');
ss(socket).on('audio-stream', function(stream, data) {
    parts = [];
    stream.on('data', function(chunk){
        parts.push(chunk);
    });
    stream.on('end', function () {
        audio.src = (window.URL || window.webkitURL).createObjectURL(new Blob(parts));
        audio.play();
    });
});

Could you do that client -> server -> client? Like a peer-to-peer voice chat? Just trying to make something for my non-programmer friends.

jojomoore2007 avatar Jan 08 '20 15:01 jojomoore2007

@jojomoore2007 For that use case I would recommend using WebRTC or something similar, it would scale much better and would be a true peer to peer implementation as opposed to the server middleman.

duncanhoggan avatar Feb 05 '20 19:02 duncanhoggan

Hello Streaming music synchronously from an mp3 file via an Angular + socket. io I have an mp3 file on my server.

And I want all my clients who visit that URL to listen to that music in sync.

That is.

Let's say the file plays for 6 minutes.

I start the song at 10:00 am

A request which comes at 10:03 am should start listening from the 3rd minute of the song.

All my clients should listen to the song in sync.

How can I achieve this with Angular and socket.io?

viradiya1993 avatar Sep 07 '21 12:09 viradiya1993

Hello Streaming music synchronously from an mp3 file via an Angular + socket. io I have an mp3 file on my server.

And I want all my clients who visit that URL to listen to that music in sync.

That is.

Let's say the file plays for 6 minutes.

I start the song at 10:00 am

A request which comes at 10:03 am should start listening from the 3rd minute of the song.

All my clients should listen to the song in sync.

How can I achieve this with Angular and socket.io?

Hi @viradiya1993 , did you get any solution for this?

bailhongalGIT avatar Apr 22 '22 08:04 bailhongalGIT

isten to the song in sync.

I also need a solution for this. does anyone know how to implement this?

Jobin-S avatar Sep 29 '23 21:09 Jobin-S