TypeError in Transport.ts, transport.produce() failing

I am trying to debug why my transport.produce() is failing on the client side and while doing so ended up in the following line:

mediasoup-client/src/Transport.ts

Line 481 in 3e9a180

const { id } = await this.safeEmitAsPromise(

this.safeEmitAsPromise comes from the EnhancedEventEmitter and in this method it calls safeEmit() which in turn calls emit() which returns a boolean.

So the destructoring of the promise return value via { id } should never be able to work.

That being said, I am not a Javascript developer so might very well be missing something but I see the browser throwing an error there.

The same problem might be in the related code for produceData().

I am not confident that my understanding of the code is 100% correct and somehow the demos are working after all. What confuses me I guess is the code in safeEmitAsPromise() which passes the resolve and reject callbacks as additional arguments to safeEmit() but that method never does anything with them apart from passing them again as args to emit() which as far as I can tell does not understand these callbacks.

I hope someone can shed a bit of light on this. Thanks!

In EnhancedEventEmitter.ts:

async safeEmitAsPromise(event: string, ...args: any[]): Promise<any>
{
	return new Promise((resolve, reject) => (
		this.safeEmit(event, ...args, resolve, reject)
	));
}

This method returns a Promise which internally emits an event with args and also with a resolve() callback and a reject() callback.

Then Transport.ts emits the “produce” event to the app using it, so the app must call the resolve() argument by passing it an object with the obtained Producer id. That’s all.

Thank you! That makes totally sense now. The combination of loose typing and callbacks with mixed await threw me off the track there. I was actually calling resolve() already in the event listener but without parameters. Issue resolve()'d :slight_smile: