question about transport.on("connect")

i have producer and consumer on a different server, when i connect to consumer before pull any streams the transport.on(“connect”) don’t seems to get fired, my question here is, is there any way on how to be sure the transport is connected before i do the consume on the transport

thanks advance

transport.on(connect) must fire, after you create transport. There seems to be some thing missing in signaling process at your side, proper debugging can help.

When you say 'you connect to consumer’s you mean you try to create transport on consumer server?

What do you mean by ‘without pulling any streams’?

sorry yeah maybe i didn’t explain it properly here, here is the flow

  1. signal from client to server to create the transport
  2. server sends back created message with the dtlsParameters
  3. webRTCDevice.createRecvTransport
  4. then on.(“connect”)

so 4 never fires unless i call a consume, but the consume will fail as the transport is not connected.

thanks for the help thats pretty much what the flow i have now, maybe i am missing something here

thanks again

Ok the main issue is transport connection, kindly check chrome://webrtc-internals to check the peer connection state you will find all the details there about why connection is not being established. If possible share screenshot here.

Possible causes can be:

  • You are not providing correct ip address in listen ips to mediasoup server.
  • you are not passing ice servers on client side, needed of you are connecting out of network.

already check those, this is like working, the issue is the connect event don’t trigger until its actually try to consume, so i don’t think its any of those you mention above

Can you show the screenshot of the peer connection from webrtc-internals?

What is the mediasoup client and server version you are using?

Does the mediasoup demo work on your local machine?

It’s my understanding that this behavior is perfectly normal.

_setupTransport internally is only called either on send or receive and if the Transport is not yet connected.

If you don’t send/receive anything, there is no need for an established PeerConnection.

Edit:
From your post I think your problem might be that you misunderstand the on connect handler?!

You’re supposed to provide a callback for the connect event listener which must use your signaling to call the server .connect() method for the according Transport with the parameter provided by mediasoup-client in the callback.

I just want to add this in case someone else struggles with this (even though I think the docs are explaining this well enough)

This is a simplified version of how the ‘connect’ procedure should look like.

Mediasoup client:

transport.on(
  'connect',
  async ({ dtlsParameters }, callback, errback) => {
    try {
      await signaling.request('connect-transport', dtlsParameters)
      callback()
    } catch (error) {
      errback(error)
    }
  }
)

Mediasoup server:

mySignaling.on('connect-transport', (dtlsParameters) => {
  const transport = getCorrectTransport() // Up to you how you get the right transport
  try {
    await transport.connect({ dtlsParameters });
  } catch (error) {
    mySignaling.throwError()
  }
}

The ‘connect’ event is fired when you call consume or produce and will establish a PeerConnection to Mediasoup server.

After you have set up this procedure it should work. Any other issues with not seeing any video or whatever are probably related to failing ICE process. In this case make sure to double-check that you use correct listenIps for WebRtcTransport or, if you use it, WebRtcServer.

Edit: Adding our code line-by-line as a reference seeing that the minified example I posted does not cover transport identification

client

connectionCallback(id: string) {
  return async ({ dtlsParameters }: any, callback: any, errback: any) => {
    try {
      await this.socket.request('mediasoup', {
        type: 'connect-transport',
        data: {
          transportId: id,
          dtlsParameters,
        },
      })
      callback()
    } catch (error: any) {
      console.warn('ERROR CONNECTING TRANSPORT', error)
      errback(error)
    }
  }
}

server

async connectTransport({
  roomId,
  transportId,
  dtlsParameters,
}: IConnectTransportRequest) {
  const router = this._routerService.getRouterBy(
    'transportId',
    roomId,
    transportId,
  );
  if (!router) return; // < async fail-safe
  
  const transport = router.appData.transports.get(transportId);
  if (transport) {
    try {
      await transport.connect({ dtlsParameters });
      return 1;
    } catch {
      console.warn('Transport connection failed');
    }
  }
  return 0; // < will throw error in intermediate API layer
}
2 Likes