dataProducer on open/close/error events

Hello! I feel a bit odd that I hadn’t noticed this before, but I seem to be unable to assign the event handlers to the dataProducer after creating it with produceData – mediasoup :: API

After calling produceData, I am returned a dataProducer object. I believe I should be able to call dataProducer.on("open", () => { console.log("Data channel opened!") }) and have the listener setup, but it doesn’t seem to be logging to console.

This is my code below, and it seems like this would work for me. I feel that the events do indeed fire, but I am assigning the listeners after they fire, thus my log doesn’t get called. Also I considered if the ordered: true/false mattered, it doesn’t seem to. I do hope these events being fired do not depend on tcp being the transport. Not sure that would be related, but this transport is going over udp by default.

          ordered: false,
          maxPacketLifeTime: 50,
          label: "SomeChannel",
          appData: { source: "someAppDataString" }
        .then(producer => {
          producer.on("open", () => {
            console.log("Movement producer open");
          producer.on("error", error => {
            console.error("Movement producer error", error);
          producer.on("close", () => {
            console.log("Movement producer closed");
        .catch(error => {
          console.error("Producing movement failed!", error);

I have also exposed the dataproducer globally, and called .close() on it. The “movement producer closed” message does not log.

I hope I’m just doing something silly, although I am assuming that the second function argument is just missing from the doc. I imagine it would read dataProducer.on(“open”, fn()); mediasoup :: API

I do get the debug log for the unrelated dataConsumers opening, but not the dataProducer opening. Is it possible that the events do not fire on the producer by design?

Any insights would be appreciated. :slight_smile:

1 Like

Depending on the browser side implementation, the datachannel maybe already open after created so no “open” event will be fired. Check readyState property.

I see that the readyState is open, and so would not fire an open event I set after that. Thank you for confirming.

Is there a recommended way to prevent the datachannel from being open before the producer is returned? Does this have to do with the callback that needs to be called in the produceData event of the transport? Or does this involve the difference between the browsers themselves? If its just a matter of writing better code on my end, I would like to do that. :smiley:

Message and Callback Queues are great for handling things synchronously.

Message queue ensures you process each message one by one. Ideal if you require callback without the next request interupting the task/process.

Callback queue basically sends a message to another server/client with a req_id/cmd_id; when the response is returned with these values it bypasses the message queue and performs the task allowing you to do many things before replying to any message.

Very necessary if for instance you want to consume a user but first need to establish pipetransport, then re-produce/re-consume and create transport, then reply to user type stuff…

This is how libwebrtc works.