about client side and server side transport


I am currently building a N:N group audio and video call using mediasoup.

I understand transport as a “path”. Therefore, when a new user enters the room, I tried to consume the producer of the new user to the receiving transport of the existing user, but the following error occurs:


2024-01-31 15:41:18 [WsExceptionsHandler] connect() already called [method:webRtcTransport.connect] - { stack: [

Should I not recycle transports? If there are 3 users, does each user need to have 1 sending transport, 2 receiving transports, and the server needs to have 3 sending transports and 9 receiving transports accordingly? I think it is a waste of resources to create transports on the server every time a new user enters.

What should I do?

It depends how you use it. One user can consume the producers of the rest of users on single transport. Or you can consume each producer on different transport. Or you can have something like create 1 transport to consumer all producers of each user like his video, audio, sharescreen. It all depends how you want to use it.

When it comes to resources, number of transports doesn’t matter it is actually the number of producers, consumers. So 10 producers, consumers on 1 transport can almost have same effect as 10 producers, consumers on 10 transports as at the end we will have 10 producers, consumers. In case of multiple transports it can use a bit more resources in calculation but that is not of any worry in my opinion.

Regarding producers it is advised to always produce on single transport as it is more optimal for bandwidth estimation. So all producers from one users should go one one transport.

As for this error, it is self explanatory you are trying to connect transport which is already connected.

In fact, I don’t know why that error occurs. I am creating one producer and consumer transport on the server for each user in the following way:

private producerTransports = new Map< string, mediasoup.types.WebRtcTransport>();

private consumerTransports = new Map< string, mediasoup.types.WebRtcTransport>();

I am connecting users to these transports, and I suspect that the problem is occurring because I am trying to connect a new user to the same transport.
So, I was wondering if I should create additional transports.

I understand that it is up to me, but if you have any other opinions, please let me know. Thank you for your reply.

You are calling connect() on a transport in which you already called connect() before. That’s the problem.

Of course you can call transport.consume() many times, but connect() must be called just once, so check your code because it’s not ok. And read the docs since this is perfectly explained in the documentation.

1 Like

This is not right, keep the transports separated for each users, you shouldn’t be connecting multiple users on single transport. You can use one transport to consume, produce multiple times for one single user not for multiple users.

First, thank you for your answers. I am referring to the official documentation and various answers in this community, but I am having difficulty understanding because I am not good at English. I am still checking your answers while translating them.

I will write down what I understand, so please let me know if it is correct.

  1. I know that when consume() is executed on the server side, recvTransport.on(“connect”) is automatically triggered on the client side. Is this correct?
    If so, what does your “you can call transport.consume() many times, but connect() must be called just once” mean?

  2. Then, as I mentioned earlier, is it good to keep one server-side transport per producer? So, the server also has 1 producer transport and n consumer transports for each user.
    If you don’t mind can I show you a little bit of my code flow and expect an answer?

I couldn’t mention it because I didn’t know how to mention many people. I’d appreciate it if you looked at the questions above.
2. is what I want to ask

Yes this is perfectly fine. Each user will have one producer transport, one consumer transport. No issues.

‘connect’ doesn’t get triggered multiple times, on each transport it will get triggered only once. So consume multiple times and connect one time

Thank you for your answer.
I will try it again and check ‘solution’ if problem solved.

1 Like

I had a question as I was implementing it according to your answer.

I am currently not receiving in the room when there is no one, and I start receiving when someone comes into the room.

User A joins: Since there are no existing users, server: consumer transport 0
User B joins: A creates recv transport for B, B creates recv transport for A, server: 2
User C joins: A->C, B->C, C->A,B, total of 4 recv transports are created, server: 6
User D joins: A->D, B->D, C->D, D->A,B,C, total of 6 recv transports are created, server: 12 …

Is this how the transport increases?

Isn’t this a mesh structure?

Does the mediasoup SFU server create transports in this way?
Is this the recommended way to proceed with development?

This look ok and the number of transports seems fine, do not worry about the number of transports as I mentioned the major things are producers, consumers. You can further verify it.

Here is another way to do the same:
User A joins: Creates it’s producing transport and produce multiple tracks on it, creates consuming transports and will consume the future producers from User B, C on this transport. Total transports 2

User B joins: Creates it’s producing transport and produce multiple tracks on it, creates consuming transports and will consume the producer from User A and future producers from User C on this transport. Total transports 2

User C joins: Creates it’s producing transport and produce multiple tracks on it, creates consuming transports and will consume the producer from User A,B and future producers from User D etc on this transport. Total transports 2
And so on…

So each user is creating only 2 transports in this approach one for producing and one for consuming irrespective of the number of producers. As there are 3 users in this call so server will have 6 transports.

The important thing is that the number of producers, consumers in this approach and the approach you mentioned remains same which is the major thing contributing to resources.

So again it is up to you how you create the transports both of these approaches are fine, the one I mentioned can give benefit in bandwidth estimation etc but the other approach is fine as well.

You were told in comments above that a client only requires a single transport to consume many producers.

Please confirm whether you have read these docs in detail or not, because otherwise this is like trying random things without any foundation:

oh finally i realized.
I thought that if i consume, i must have to connect one more time. so i think this error occurred because of this reason… I think now I understand what Mr. ibc said before

2024-01-31 15:41:18 [WsExceptionsHandler] connect() already called [method:webRtcTransport.connect] - { stack: [

Does it mean that if you only consume() in a connected state, you can communicate with a new user? is it right?

so, in other words, the way to connect() two people at first time and then only consume when a new user comes in. is my understanding correct?

to. ibc
I was implementing based on the contents of the link you gave me. But as I said earlier, I think I lacked understanding because the language was different. Thank you

You shouldn’t worry about the connected state before consuming, producing. Whenever you will try to consume, produce then mediasoup will automatically fire ‘connect’ event on transport if needed which is usually the first time you produce, consume on that specific transport. Whenever that ‘connect’ event fires you are supposed to signal the server and call connect() on server side as well, this is the rule of thumb whenever connect event fires just connect the transport. Consume, Produce without the worry of connection and mediasoup knows when you fire the connect event on transport and then if you have handled that connect event properly the connection will happen without any issue.

Not at all. It looks like if you think that “connect” a transport is about connecting 2 users but that’s totally wrong. It’s not that.

  • you create a receiving transport in server side.
  • you create a receiving transport in client side.
  • you call transport .consume(producer1) in server side.
  • you call transport,consume(params) in client side.
  • Client side transport emits “connect”, you send to server can call transport.connect() in server side transport.
  • that’s all.

Later you want to also consume profucer2 on same recv transport:

  • you call transport .consume(producer2) in server side.
  • you call transport,consume(params) in client side.
  • done.

I perfectly understand it all. thanks to you guys.
I am really really grateful for your kind and helpful answer. This has been a tremendous help to me.
I’m so sorry that I can’t check multiple solution check boxes.
Thank you so much to both of you again.

1 Like