MixinNetwork / kraken

🐙 High performance WebRTC SFU implemented with pure Go.
Apache License 2.0
334 stars 49 forks source link

Use server-sent events to push remote SDP instead of polling with subscribe request #8

Open deleolajide opened 4 years ago

deleolajide commented 4 years ago

It might be more efficent to use server-sent events to push the SDP of remote peer connections to a participant instead of polling for them with subscribe requests using JavasScript setTimeout.

I am a Java developer and not a Go developer otherwise, I would have sent you a PR using one of the available Go implementations eventsource or go-sse

BTW, I am a fan of your work and I love your blogs. I appreciate your contributions to real-time communications even though WebRTC is not your main focus and English is probably not your only language of communication.

cedricfung commented 4 years ago

I have thought about these real-time updates from server, but the core of kraken is simplicity, so I try to minimize the feature set and try the best to make its API more straight forward for developers.

We use kraken in production in our Mixin Messenger app, and we wrap the kraken API with our server. Our server will provide both authentication and real-time updates upon the kraken API.

Thanks and yes my native language is Chinese as I have written in my first blog post.

deleolajide commented 4 years ago

We use kraken in production in our Mixin Messenger app, and we wrap the kraken API with our serve

I am using Kraken with Openfire and using XMPP to do the real-time updates from users joining/leaving the MUC associated with the audiobridge and wont need to do the polling in my application.

However, to make a Kraken web client even simpler for developers (especially those who have have experience of implementing signalling with webrtc), a push mechanism using a web standard like server-sent will be considered more elegant in the long run.

async function subscribe(pc) {
  await rpc('subscribe', [roomId, userId, trackId]);
  var sse = new EventSource('http://localhost:7000/sse?id=trackId'); 

  sse.onmessage = function(res) {
    await pc.setRemoteDescription(res.data);
    var sdp = await pc.createAnswer();
    await pc.setLocalDescription(sdp);
    // RPC anwser the subscribe offer
    await rpc('answer', [roomId, userId, trackId, JSON.stringify(sdp)]);
  };
}

is simpler and more elegant than

async function subscribe(pc) {
  var res = await rpc('subscribe', [roomId, userId, trackId]);

  if (res.data && res.data.type === 'offer') {
    await pc.setRemoteDescription(res.data);
    var sdp = await pc.createAnswer();
    await pc.setLocalDescription(sdp);
    // RPC anwser the subscribe offer
    await rpc('answer', [roomId, userId, trackId, JSON.stringify(sdp)]);
  }
  setTimeout(function () {
    subscribe(pc);
  }, 3000);
}

Don't you think?

cedricfung commented 4 years ago

Reasonable, I will try some code and if it won't make the project code over complicated I will add this feature.