mediasoup v3 API

mediasoup

The top-level exported module.

const mediasoup = require("mediasoup");

// Or using destructuring assignment.
const {
  types,
  version,
  observer,
  createWorker,
  getSupportedRtpCapabilities,
  parseScalabilityMode
} = require("mediasoup");

Properties

mediasoup.types

An Object holding all classes and TypeScript types exported by mediasoup.

@type Object, read only

import { types as mediasoupTypes } from "mediasoup";

let worker: mediasoupTypes.Worker;
let rtpParameters: mediasoupTypes.RtpParameters;

// or alternatively:

import { Worker, RtpParameters } from "mediasoup/lib/types";

let worker: Worker;
let rtpParameters: RtpParameters;

mediasoup.version

The mediasoup version.

@type String, read only

console.log(mediasoup.version);
// => "3.0.0"

mediasoup.observer

An event emitter that allows the application (or third party libraries) monitor Worker instances created by the application. See the Observer Events section below.

@type EventEmitter, read only

Functions

mediasoup.createWorker(settings)

Creates a new worker with the given settings.

ArgumentTypeDescriptionRequiredDefault
settingsWorkerSettingsWorker settings.No 

@async

@returns Worker

const worker = await mediasoup.createWorker(
  {
    logLevel            : "warn",
    dtlsCertificateFile : "/home/foo/dtls-cert.pem",
    dtlsPrivateKeyFile  : "/home/foo/dtls-key.pem"
  });

mediasoup.getSupportedRtpCapabilities()

Returns a cloned copy of the mediasoup supported RTP capabilities, specifically the content of the mediasoup/src/supportedRtpCapabilities.ts file.

@returns RtpCapabilities

const rtpCapabilities = mediasoup.getSupportedRtpCapabilities();

console.log(rtpCapabilities);
// => { codecs: [...], headerExtensions: [...] }

Those are NOT the RTP capabilities needed by mediasoup-client's device.load() and libmediasoupclient's device.Load() methods. There you must use router.rtpCapabilities getter instead.

mediasoup.parseScalabilityMode(scalabilityMode)

Parses the given scalabilityMode string according to the rules in webrtc-svc.

ArgumentTypeDescriptionRequiredDefault
scalabilityModeStringScalability mode.No 

@returns ScalabilityMode:

  • spatialLayers {@type Number} Number of spatial layers (by default 1).

  • temporalLayers {@type Number} Number of temporal layers (by default 1).

mediasoup.parseScalabilityMode("L2T3");
// => { spatialLayers: 2, temporalLayers: 3 }

mediasoup.parseScalabilityMode("S3T3");
// => { spatialLayers: 3, temporalLayers: 3 }

mediasoup.parseScalabilityMode("L4T7_KEY_SHIFT");
// => { spatialLayers: 4, temporalLayers: 7 }

mediasoup.parseScalabilityMode(undefined);
// => { spatialLayers: 1, temporalLayers: 1 }

Observer Events

See the Observer API section below.

mediasoup.observer.on(“newworker”, fn(worker))

Emitted when a new worker is created.

ArgumentTypeDescription
workerWorkerNew worker.
mediasoup.observer.on("newworker", (worker) =>
{
  console.log("new worker created [pid:%d]", worker.pid);
});

Worker

A worker represents a mediasoup C++ subprocess that runs in a single CPU core and handles Router instances.

Dictionaries

WorkerSettings

FieldTypeDescriptionRequiredDefault
logLevelWorkerLogLevelLogging level for logs generated by the media worker subprocesses (check the Debugging documentation). Valid values are “debug”, “warn”, “error” and “none”.No“error”
logTagsArray<WorkerLogTag>Log tags for debugging. Check the list of available tags in Debugging documentation.No[ ]
rtcMinPortNumberMinimun RTC port for ICE, DTLS, RTP, etc.No10000
rtcMaxPortNumberMaximum RTC port for ICE, DTLS, RTP, etc.No59999
dtlsCertificateFileStringPath to the DTLS public certificate file in PEM format. If unset, a certificate is dynamically created.No 
dtlsPrivateKeyFileStringPath to the DTLS certificate private key file in PEM format. If unset, a certificate is dynamically created.No 
appDataObjectCustom application data.No{ }

RTC listening IPs are not set at worker level. Instead, they are set per individual transport.

WorkerUpdateableSettings

FieldTypeDescriptionRequiredDefault
logLevelStringLogging level for logs generated by the media worker subprocesses (check the Debugging documentation). Valid values are “debug”, “warn”, “error” and “none”.No“error”
logTagsArray<String>Log tags for debugging. Check the list of available tags in Debugging documentation.No 

WorkerResourceUsage

An object with the fields of the uv_rusage_t struct.

Both ru_utime and ru_stime values are given in milliseconds.

Enums

WorkerLogLevel

ValueDescription
“debug”Log all severities.
“warn”Log “warn” and “error” severities.
“error”Log “error” severity.
“none”Do not log anything.

WorkerLogTag

ValueDescription
“info”Logs about software/library versions, configuration and process information.
“ice”Logs about ICE.
“dtls”Logs about DTLS.
“rtp”Logs about RTP.
“srtp”Logs about SRTP encryption/decryption.
“rtcp”Logs about RTCP.
“rtx”Logs about RTP retransmission, including NACK/PLI/FIR.
“bwe”Logs about transport bandwidth estimation.
“score”Logs related to the scores of Producers and Consumers.
“simulcast”Logs about video simulcast.
“svc”Logs about video SVC.
“sctp”Logs about SCTP (DataChannel).
“message”Logs about messages (can be SCTP messages or direct messages).

Properties

worker.pid

The PID of the worker process.

@type Number, read only

console.log(worker.pid);
// => 86665

worker.closed

Whether the worker is closed.

@type Boolean, read only

console.log(worker.closed);
// => false

worker.appData

Custom data Object provided by the application in the worker factory method. The app can modify its content at any time.

@type Object, read only

worker.observer

See the Observer Events section below.

@type EventEmitter, read only

Methods

worker.close()

Closes the worker. Triggers a “workerclose” event in all its routers.

worker.getResourceUsage()

Provides resource usage of the mediasoup-worker subprocess.

@async

@returns WorkerResourceUsage

const usage = await worker.getResourceUsage();

// =>
{
  ru_idrss: 0,
  ru_inblock: 0,
  ru_isrss: 0,
  ru_ixrss: 0,
  ru_majflt: 0,
  ru_maxrss: 46047232,
  ru_minflt: 11446,
  ru_msgrcv: 23641,
  ru_msgsnd: 40005,
  ru_nivcsw: 27926,
  ru_nsignals: 0,
  ru_nswap: 0,
  ru_nvcsw: 0,
  ru_oublock: 0,
  ru_stime: 1026,
  ru_utime: 3066
}

worker.updateSettings(settings)

Updates the worker settings in runtime. Just a subset of the worker settings can be updated.

ArgumentTypeDescriptionRequiredDefault
settingsWorkerUpdateableSettingsWorker updateable settings.No 

@async

await worker.updateSettings({ logLevel: "warn" });

worker.createRouter(options)

Creates a new router.

ArgumentTypeDescriptionRequiredDefault
optionsRouterOptionsRouter options.Yes 

@async

@returns Router

const mediaCodecs =
[
  {
    kind        : "audio",
    mimeType    : "audio/opus",
    clockRate   : 48000,
    channels    : 2
  },
  {
    kind       : "video",
    mimeType   : "video/H264",
    clockRate  : 90000,
    parameters :
    {
      "packetization-mode"      : 1,
      "profile-level-id"        : "42e01f",
      "level-asymmetry-allowed" : 1
    }
  }
];

const router = await worker.createRouter({ mediaCodecs });

Events

worker.on(“died”, fn(error))

Emitted when the worker process unexpectedly dies.

ArgumentTypeDescription
errorErrorOriginating error.

This should never happens. If it happens, it's a bug. Please report it following these instructions.

worker.on("died", (error) =>
{
  console.error("mediasoup worker died!: %o", error);
});

Observer Events

See the Observer API section below.

worker.observer.on(“close”, fn())

Emitted when the worker is closed for whatever reason.

worker.observer.on(“newrouter”, fn(router))

Emitted when a new router is created.

ArgumentTypeDescription
routerRouterNew router.
worker.observer.on("newrouter", (router) =>
{
  console.log("new router created [id:%s]", router.id);
});

Router

A router enables injection, selection and forwarding of media streams through Transport instances created on it.

Developers may think of a mediasoup router as if it were a “multi-party conference room”, although mediasoup is much more low level than that and doesn't constrain itself to specific high level use cases (for instance, a “multi-party conference room” could involve various mediasoup routers, even in different physicals hosts).

Dictionaries

RouterOptions

FieldTypeDescriptionRequiredDefault
mediaCodecsArray<RtpCodecCapability>Router media codecs.No[ ]
appDataObjectCustom application data.No{ }
  • Feature codecs such as RTX MUST NOT be placed into the mediaCodecs list.
  • If preferredPayloadType is given in a RtpCodecCapability (although it's unnecessary) it's extremely recommended to use a value in the 96-127 range.

PipeToRouterOptions

FieldTypeDescriptionRequiredDefault
mediaCodecsArray<RtpCodecCapability>Router media codecs.No[ ]
producerIdStringProducer idNo 
dataProducerIdStringData producer idNo 
routerRouterDestination router to pipe the given producer.Yes 
listenIpStringIP to connect both routers in the same host.No“127.0.0.1”
enableSctpBooleanCreate a SCTP association.Notrue
numSctpStreamsNumSctpStreamsSCTP streams number.No 
enableRtxBooleanEnable RTX and NACK for RTP retransmission. Typically not needed since the link is typically localhost.Nofalse
enableSrtpBooleanEnable SRTP.Nofalse
  • Only one of producerId and dataProducerId must be provided.
  • SCTP arguments will only apply the first time the underlying transports are created.

PipeToRouterResult

FieldTypeDescriptionRequiredDefault
pipeConsumerConsumerThe consumer created in the current router.No 
pipeProducerProducerThe producer created in the target router.No 
pipeDataConsumerDataConsumerThe data consumer created in the current router.No 
pipeDataProducerDataProducerThe data producer created in the target router.No 

Properties

router.id

Router identifier.

@type String, read only

console.log(router.id);
// => "15177e19-5665-4eba-9a6a-c6cf3db16259"

router.closed

Whether the router is closed.

@type Boolean, read only

router.rtpCapabilities

An Object with the RTP capabilities of the router. These capabilities are tipically needed by mediasoup clients to compute their sending RTP parameters.

@type RtpCapabilities, read only

router.appData

Custom data Object provided by the application in the router factory method. The app can modify its content at any time.

@type Object, read only

router.observer

See the Observer Events section below.

@type EventEmitter, read only

Methods

router.close()

Closes the router. Triggers a “routerclose” event in all its transports and also “routerclose” event in all its RTP observers.

router.createWebRtcTransport(options)

Creates a new WebRTC transport.

ArgumentTypeDescriptionRequiredDefault
optionsWebRtcTransportOptionsWebRTC transport options.Yes 

@async

@returns WebRtcTransport

const transport = await router.createWebRtcTransport(
  {
    listenIps : [ { ip: "192.168.0.111", announcedIp: "88.12.10.41" } ],
    enableUdp : true,
    enableTcp : true,
    preferUdp : true
  });

router.createPlainRtpTransport(options) (DEPRECATED)

createPlainRtpTransport() has been renamed to createPlainTransport() since mediasoup version 3.5.0.

router.createPlainTransport(options)

Creates a new plain transport.

ArgumentTypeDescriptionRequiredDefault
optionsPlainTransportOptionsPlain transport options.Yes 

@async

@returns PlainTransport

const transport = await router.createPlainTransport(
  {
    listenIp : "a1:22:aA::08",
    rtcpMux  : true,
    comedia  : true
  });

router.createPipeTransport(options)

Creates a new pipe transport.

ArgumentTypeDescriptionRequiredDefault
optionsPipeTransportOptionsPipe transport options.Yes 

@async

@returns PipeTransport

const transport = await router.createPipeTransport(
  {
    listenIp : "192.168.1.33"
  });

router.createDirectTransport(options)

Creates a new direct transport.

ArgumentTypeDescriptionRequiredDefault
optionsDirectTransportOptionsPlain transport options.Yes 

@async

@returns DirectTransport

const transport = await router.createDirectTransport();

router.pipeToRouter({ producerId, dataProducerId, router, listenIp })

Pipes the given media or data producer into another router in the same host. It creates an underlying PipeTransport (if not previously created) that interconnects both routers.

This is specially useful to expand broadcasting capabilities (one to many) by interconnecting different routers that run in separate workers (so in different CPU cores).

ArgumentTypeDescriptionRequiredDefault
optionsPipeToRouterOptionsOptionsYes 

@async

@returns PipeToRouterResult

// Have two workers.
const worker1 = await mediasoup.createWorker();
const worker2 = await mediasoup.createWorker();

// Create a router in each worker.
const router1 = await worker1.createRouter({ mediaCodecs });
const router2 = await worker2.createRouter({ mediaCodecs });

// Produce in router1.
const transport1 = await router1.createWebRtcTransport({ ... });
const producer1 = await transport1.produce({ ... });

// Pipe producer1 into router2.
await router1.pipeToRouter({ producerId: producer1.id, router: router2 });

// Consume producer1 from router2.
const transport2 = await router2.createWebRtcTransport({ ... });
const consumer2 = await transport2.consume({ producerId: producer1.id, ... });

router.createAudioLevelObserver(options)

Creates a new audio level observer.

ArgumentTypeDescriptionRequiredDefault
optionsAudioLevelObserverOptionsOptions.Yes 

@async

@returns AudioLevelObserver

const audioLevelObserver = await router.createAudioLevelObserver(
  {
    maxEntries : 1,
    threshold  : -70,
    interval   : 2000
  });

router.canConsume({ producerId, rtpCapabilities })

Whether the given RTP capabilities are valid to consume the given producer.

ArgumentTypeDescriptionRequiredDefault
producerIdStringProducer id.Yes 
rtpCapabilitiesRtpCapabilitiesRTP capabilities of the potential consumer.Yes 

@returns Boolean

if (router.canConsume({ producerId, rtpCapabilities }))
{
  // Consume the producer by calling transport.consume({ producerId, rtpCapabilities }).
}

Events

router.on(“workerclose”, fn())

Emitted when the worker this router belongs to is closed for whatever reason. The router itself is also closed. A “routerclose” event is triggered in all its transports and a “routerclose” event is triggered in all its RTP observers.

router.on("workerclose", () =>
{
  console.log("worker closed so router closed");
});

Observer Events

See the Observer API section below.

router.observer.on(“close”, fn())

Emitted when the router is closed for whatever reason.

router.observer.on(“newtransport”, fn(transport))

Emitted when a new transport is created.

ArgumentTypeDescription
transportTransportNew transport.
router.observer.on("newtransport", (transport) =>
{
  console.log("new transport created [id:%s]", transport.id);
});

router.observer.on(“newrtpobserver”, fn(rtpObserver))

Emitted when a new RTP observer is created.

ArgumentTypeDescription
rtpObserverRtpObserverNew RTP observer.
router.observer.on("newrtpobserver", (rtpObserver) =>
{
  console.log("new RTP observer created [id:%s]", rtpObserver.id);
});

Transport

@abstract

A transport connects an endpoint with a mediasoup router and enables transmission of media in both directions by means of ProducerConsumerDataProducer and DataConsumer instances created on it.

mediasoup implements the following transport classes:

Dictionaries

TransportListenIp

FieldTypeDescriptionRequiredDefault
ipStringListening IPv4 or IPv6.Yes 
announcedIpStringAnnounced IPv4 or IPv6 (useful when running mediasoup behind NAT with private IP).No 

If you use “0.0.0.0” or “::” as ip value, then you need to also provide announcedIp.

TransportTuple

FieldTypeDescriptionRequiredDefault
localIpStringLocal IP address.Yes 
localPortNumberLocal port.Yes 
remoteIpStringRemote IP address.No 
remotePortNumberRemote port.No 
protocolStringProtocol (“udp” / “tcp”).Yes 

Both remoteIp and remotePort are unset until the media address of the remote endpoint is known, which happens after calling transport.connect() in PlainTransport and PipeTransport, or via dynamic detection as it happens in WebRtcTransport (in which the remote media address is detected by ICE means), or in PlainTransport (when using comedia mode).

TransportTraceEventData

FieldTypeDescriptionRequiredDefault
typeTransportTraceEventTypeTrace event type.Yes 
timestampNumberEvent timestamp.Yes 
directionString“in” (icoming direction) or “out” (outgoing direction).Yes 
infoObjectPer type specific information.Yes 

See also “trace” Event in the Debugging section.

Enums

TransportTraceEventType

ValueDescription
“probation”RTP probation packet.
“bwe”Transport bandwidth estimation changed.

TransportSctpState

ValueDescription
“new”SCTP procedures not yet initiated.
“connecting”SCTP connecting.
“connected”SCTP successfully connected.
“failed”SCTP connection failed.
“closed”SCTP state when the transport has been closed.

Properties

These are properties common to all transport classes. Each transport class may define new ones.

transport.id

Transport identifier.

@type String, read only

transport.closed

Whether the transport is closed.

@type Boolean, read only

transport.appData

Custom data Object provided by the application in the transport factory method. The app can modify its content at any time.

@type Object, read only

transport.appData.foo = "bar";

transport.observer

See the Observer Events section below.

@type EventEmitter, read only

Methods

These are methods common to all transport classes. Each transport class may define new ones.

transport.close()

Closes the transport. Triggers a “transportclose” event in all its producers and also “transportclose” event in all its consumers.

transport.getStats()

Returns current RTC statistics of the transport. Each transport class produces a different set of statistics.

@async

@abstract

@returns Array<Object>

Check the RTC Statistics section for more details.

transport.connect()

Provides the transport with the remote endpoint's transport parameters. Each transport class requires specific arguments in this method. Check the connect() method in each one of them.

@async

@abstract

transport.setMaxIncomingBitrate(bitrate)

Set maximum incoming bitrate for media streams sent by the remote endpoint over this transport.

ArgumentTypeDescriptionRequiredDefault
bitrateNumberMaximum sending bitrate in bps.Yes0 (no limit)

@async

This method just works when REMB is available in the remote sender, which is typically just supported in WebRTC.

await transport.setMaxIncomingBitrate(3500000);

transport.produce(options)

Instructs the router to receive audio or video RTP (or SRTP depending on the transport class). This is the way to inject media into mediasoup.

ArgumentTypeDescriptionRequiredDefault
optionsProducerOptionsProducer options.Yes 

@async

@returns Producer

Check the RTP Parameters and Capabilities section for more details.

const producer = await transport.produce(
  {
    kind          : "video",
    rtpParameters :
    {
      mid    : "1",
      codecs :
      [
        {
          mimeType    : "video/VP8",
          payloadType : 101,
          clockRate   : 90000,
          rtcpFeedback :
          [
            { type: "nack" },
            { type: "nack", parameter: "pli" },
            { type: "ccm", parameter: "fir" },
            { type: "goog-remb" }
          ]
        },
        {
          mimeType    : "video/rtx",
          payloadType : 102,
          clockRate   : 90000,
          parameters  : { apt: 101 }
        }
      ],
      headerExtensions :
      [
        {
          id  : 2, 
          uri : "urn:ietf:params:rtp-hdrext:sdes:mid"
        },
        { 
          id  : 3, 
          uri : "urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id"
        },
        { 
          id  : 5, 
          uri: "urn:3gpp:video-orientation" 
        },
        { 
          id  : 6, 
          uri : "http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time"
        }
      ],
      encodings :
      [
        { rid: "r0", active: true, maxBitrate: 100000 },
        { rid: "r1", active: true, maxBitrate: 300000 }
        { rid: "r2", active: true, maxBitrate: 900000 }
      ],
      rtcp :
      {
        cname : "Zjhd656aqfoo"
      }
    }
  });

transport.consume(options)

Instructs the router to send audio or video RTP (or SRTP depending on the transport class). This is the way to extract media from mediasoup.

ArgumentTypeDescriptionRequiredDefault
optionsConsumerOptionsConsumer options.Yes 

@async

@returns Consumer

Check the RTP Parameters and Capabilities section for more details.

When creating a consumer it's recommended to set paused to true, then transmit the consumer parameters to the consuming endpoint and, once the consuming endpoint has created its local side consumer, unpause the server side consumer using the resume() method.

Reasons for create the server side consumer in paused mode:

  • If the remote endpoint is a WebRTC browser or application and it receives a RTP packet of the new consumer before the remote RTCPeerConnection is ready to process it (this is, before the remote consumer is created in the remote endpoint) it may happen that the RTCPeerConnection will wrongly associate the SSRC of the received packet to an already existing SDP m= section, so the imminent creation of the new consumer and its associated m= section will fail.
  • Also, when creating a video consumer, this is an optimization to make it possible for the consuming endpoint to render the video as far as possible. If the server side consumer was created with paused: false, mediasoup will immediately request a key frame to the producer and that key frame may reach the consuming endpoint even before it's ready to consume it, generating “black” video until the device requests a keyframe by itself.
const consumer = await transport.consume(
  {
    producerId      : "a7a955cf-fe67-4327-bd98-bbd85d7e2ba3",
    rtpCapabilities :
    {
      codecs :
      [
        {
          mimeType             : "audio/opus",
          kind                 : "audio",
          clockRate            : 48000,
          preferredPayloadType : 100,
          channels             : 2
        },
        {
          mimeType             : "video/H264",
          kind                 : "video",
          clockRate            : 90000,
          preferredPayloadType : 101,
          rtcpFeedback         :
          [
            { type: "nack" },
            { type: "nack", parameter: "pli" },
            { type: "ccm", parameter: "fir" },
            { type: "goog-remb" }
          ],
          parameters :
          {
            "level-asymmetry-allowed" : 1,
            "packetization-mode"      : 1,
            "profile-level-id"        : "4d0032"
          }
        },
        {
          mimeType             : "video/rtx",
          kind                 : "video",
          clockRate            : 90000,
          preferredPayloadType : 102,
          rtcpFeedback         : [],
          parameters           :
          {
            apt : 101
          }
        }
      ],
      headerExtensions :
      [
        {
          kind             : "video",
          uri              : "http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time", // eslint-disable-line max-len
          preferredId      : 4,
          preferredEncrypt : false
        },
        {
          kind             : "audio",
          uri              : "urn:ietf:params:rtp-hdrext:ssrc-audio-level",
          preferredId      : 8,
          preferredEncrypt : false
        },
        {
          kind             : "video",
          uri              : "urn:3gpp:video-orientation",
          preferredId      : 9,
          preferredEncrypt : false
        },
        {
          kind             : "video",
          uri              : "urn:ietf:params:rtp-hdrext:toffset",
          preferredId      : 10,
          preferredEncrypt : false
        }
      ]
    }
  });

transport.produceData(options)

Instructs the router to receive data messages. Those messages can be delivered by an endpoint via SCTP protocol (AKA DataChannel in WebRTC) or can be directly sent from the Node.js application if the transport is a DirectTransport.

ArgumentTypeDescriptionRequiredDefault
optionsDataProducerOptionsData producer options.No{ }

@async

@returns DataProducer

// Using SCTP:
const dataProducer = await transport.produceData(
  {
    sctpStreamParameters :
    {
      streamId : 4,
      ordered  : true
    },
    label : 'foo'
  });

// Using a direct transport:
const dataProducer = await transport.produceData();

transport.consumeData(options)

Instructs the router to send data messages to the endpoint via SCTP protocol (AKA DataChannel in WebRTC) or directly to the Node.js process if the transport is a DirectTransport.

ArgumentTypeDescriptionRequiredDefault
optionsDataConsumerOptionsData Consumer options.Yes 

@async

@returns DataConsumer

const dataConsumer = await transport.consumeData(
  {
    dataProducerId : "a7a955cf-fe67-4327-bd98-bbd85d7e2ba4"
  });

transport.enableTraceEvent(types)

Instructs the transport to emit “trace” events. For monitoring purposes. Use with caution.

ArgumentTypeDescriptionRequiredDefault
typesArray<TransportTraceEventType>Enabled types.NoUnset (so disabled)

@async

await transport.enableTraceEvent([ "probation" ]);

transport.on("trace", (trace) =>
{
  // trace.type can just be "probation".
});

Events

These are events common to all transport classes. Each transport class may define new ones.

transport.on(“routerclose”, fn())

Emitted when the router this transport belongs to is closed for whatever reason. The transport itself is also closed. A “transportclose” event is triggered in all its producers and a “transportclose” event is triggered in all its consumers.

transport.on("routerclose", () =>
{
  console.log("router closed so transport closed");
});

transport.on(“trace”, fn(trace))

See enableTraceEvent() method.

ArgumentTypeDescription
traceTransportTraceEventDataTrace data.
transport.on("trace", (trace) =>
{
  console.log(trace);
});

Observer Events

See the Observer API section below.

These are observer events common to all transport classes. Each transport class may define new ones.

transport.observer.on(“close”, fn())

Emitted when the transport is closed for whatever reason.

transport.observer.on(“newproducer”, fn(producer))

Emitted when a new producer is created.

ArgumentTypeDescription
producerProducerNew producer.
transport.observer.on("newproducer", (producer) =>
{
  console.log("new producer created [id:%s]", producer.id);
});

transport.observer.on(“newconsumer”, fn(consumer))

Emitted when a new consumer is created.

ArgumentTypeDescription
consumerConsumerNew consumer.
transport.observer.on("newconsumer", (consumer) =>
{
  console.log("new consumer created [id:%s]", consumer.id);
});

transport.observer.on(“newdataproducer”, fn(dataProducer))

Emitted when a new data producer is created.

ArgumentTypeDescription
dataProducerDataProducerNew producer.
transport.observer.on("newdataproducer", (dataProducer) =>
{
  console.log("new data producer created [id:%s]", dataProducer.id);
});

transport.observer.on(“newdataconsumer”, fn(dataConsumer))

Emitted when a new data consumer is created.

ArgumentTypeDescription
dataConsumerDataConsumerNew consumer.
transport.observer.on("newdataconsumer", (dataConsumer) =>
{
  console.log("new data consumer created [id:%s]", dataConsumer.id);
});

transport.observer.on(“trace”, fn(trace))

Same as the trace event.

WebRtcTransport

@inherits Transport

A WebRTC transport represents a network path negotiated by both, a WebRTC endpoint and mediasoup, via ICE and DTLS procedures. A WebRTC transport may be used to receive media, to send media or to both receive and send. There is no limitation in mediasoup. However, due to their design, mediasoup-client and libmediasoupclient require separate WebRTC transports for sending and receiving.

The WebRTC transport implementation of mediasoup is ICE Lite, meaning that it does not initiate ICE connections but expects ICE Binding Requests from endpoints.

Dictionaries

WebRtcTransportOptions

FieldTypeDescriptionRequiredDefault
listenIpsArray<TransportListenIp|String>Listening IP address or addresses in order of preference (first one is the preferred one).Yes 
enableUdpBooleanListen in UDP.Notrue
enableTcpBooleanListen in TCP.Nofalse
preferUdpBooleanListen in UDP.Nofalse
preferTcpBooleanListen in TCP.Nofalse
initialAvailableOutgoingBitrateNumberInitial available outgoing bitrate (in bps).No600000
enableSctpBooleanCreate a SCTP association.Nofalse
numSctpStreamsNumSctpStreamsSCTP streams number.No 
maxSctpMessageSizeNumberMaximum allowed size for SCTP messages sent by DataProducers.No262144
sctpSendBufferSizeNumberSCTP send buffer size used by usrsctp.NO262144
appDataObjectCustom application data.No{ }
  • Do not use “0.0.0.0” into listenIps. Values in listenIps must be specific bindable IPs in the host.
  • If you use “0.0.0.0” or “::” into listenIps, then you need to also provide announcedIp in the corresponding entry in listenIps.
  • initialAvailableOutgoingBitrate is just applied when the consumer endpoint supports REMB or Transport-CC.

IceParameters

FieldTypeDescriptionRequiredDefault
usernameFragmentStringICE username fragment.No 
passwordStringICE password.No 
iceLiteBooleanICE Lite.No 

IceCandidate

FieldTypeDescriptionRequiredDefault
foundationStringUnique identifier that allows ICE to correlate candidates that appear on multiple transports.Yes 
priorityNumberThe assigned priority of the candidate.Yes 
ipStringThe IP address of the candidate.Yes 
protocolStringThe protocol of the candidate (“udp” / “tcp”).Yes 
portNumberThe port for the candidate.Yes 
typeStringThe type of candidate (always “host”).Yes 
tcpTypeStringThe type of TCP candidate (always “passive”).No 

DtlsParameters

FieldTypeDescriptionRequiredDefault
roleDtlsRoleDTLS role.No“auto”
fingerprintsArray<DtlsFingerprint>DTLS fingerprints.Yes 

DtlsFingerprint

The hash function algorithm (as defined in the “Hash function Textual Names” registry initially specified in RFC 4572 Section 8) and its corresponding certificate fingerprint value (in lowercase hex string as expressed utilizing the syntax of “fingerprint” in RFC 4572 Section 5).

FieldTypeDescriptionRequiredDefault
algorithmStringHash function algorithm.Yes 
value StringCertificate fingerprint value.Yes 

Enums

IceState

ValueDescription
“new”No ICE Binding Requests have been received yet.
“connected”Valid ICE Binding Request have been received, but none with USE-CANDIDATE attribute. Outgoing media is allowed.
“completed”ICE Binding Request with USE_CANDIDATE attribute has been received. Media in both directions is now allowed.
“disconnected”ICE was “connected” or “completed” but it has suddenly failed (this can just happen if the selected tuple has “tcp” protocol).
“closed”ICE state when the transport has been closed.

DtlsRole

ValueDescription
“auto”The DTLS role is determined based on the resolved ICE role (the “controlled” role acts as DTLS client, the “controlling” role acts as DTLS server”). Since mediasoup is a ICE Lite implementation it always behaves as ICE “controlled”.
“client”DTLS client role.
“server”DTLS server role.

DtlsState

ValueDescription
“new”DTLS procedures not yet initiated.
“connecting”DTLS connecting.
“connected”DTLS successfully connected (SRTP keys already extracted).
“failed”DTLS connection failed.
“closed”DTLS state when the transport has been closed.

Properties

See also Transport Properties.

webRtcTransport.iceRole

Local ICE role. Due to the mediasoup ICE Lite design, this is always “controlled”.

@type String, read only

webRtcTransport.iceParameters

Local ICE parameters.

@type IceParameters, read only

webRtcTransport.iceCandidates

Local ICE candidates.

@type Array<IceCandidate>, read only

webRtcTransport.iceState

Current ICE state.

@type IceState, read only

webRtcTransport.iceSelectedTuple

The selected transport tuple if ICE is in “connected” or “completed” state. It is undefined if ICE is not established (no working candidate pair was found).

@type TransportTuple, read only

webRtcTransport.dtlsParameters

Local DTLS parameters.

@type DtlsParameters, read only

webRtcTransport.dtlsState

Current DTLS state.

@type DtlsState, read only

webRtcTransport.dtlsRemoteCert

The remote certificate in PEM format. It is set once the DTLS state becomes “connected”.

@type String, read only

The application may want to inspect the remote certificate for authorization purposes by using some certificates utility such as the Node pem module.

webRtcTransport.sctpParameters

Local SCTP parameters. Or undefined if SCTP is not enabled.

@type SctpParameters, read only

webRtcTransport.sctpState

Current SCTP state. Or undefined if SCTP is not enabled.

@type TransportSctpState, read only

Methods

See also Transport Methods.

webRtcTransport.getStats()

Returns current RTC statistics of the WebRTC transport.

@async

@override

@returns Array<ProducerStat>

Check the RTC Statistics section for more details.

webRtcTransport.connect({ dtlsParameters })

Provides the WebRTC transport with the endpoint parameters.

ArgumentTypeDescriptionRequiredDefault
dtlsParametersDtlsParametersRemote DTLS parameters.Yes 

@async

@overrides

await webRtcTransport.connect(
  {
    dtlsParameters :
    {
      role         : "server",
      fingerprints :
      [
        {
          algorithm : "sha-256",
          value     : "E5:F5:CA:A7:2D:93:E6:16:AC:21:09:9F:23:51:62:8C:D0:66:E9:0C:22:54:2B:82:0C:DF:E0:C5:2C:7E:CD:53"
        }
      ]
    }
  });

webRtcTransport.restartIce()

Restarts the ICE layer by generating new local ICE parameters that must be signaled to the remote endpoint.

@async

@returns IceParameters

const iceParameters = await webRtcTransport.restartIce();

// Send the new ICE parameters to the endpoint.

Events

See also Transport Events.

webRtcTransport.on(“icestatechange”, fn(iceState))

Emitted when the transport ICE state changes.

ArgumentTypeDescription
iceStateIceStateNew ICE state.
webRtcTransport.on("icestatechange", (iceState) =>
{
  console.log("ICE state changed to %s", iceState);
});

webRtcTransport.on(“iceselectedtuplechange”, fn(iceSelectedTuple))

Emitted after ICE state becomes “completed” and when the ICE selected tuple changes.

ArgumentTypeDescription
iceSelectedTupleTransportTupleThe new ICE selected tuple.

webRtcTransport.on(“dtlsstatechange”, fn(dtlsState))

Emitted when the transport DTLS state changes.

ArgumentTypeDescription
dtlsStateDtlsStateThe new DTLS state.

webRtcTransport.on(“sctpstatechange”, fn(sctpState))

Emitted when the transport SCTP state changes.

ArgumentTypeDescription
sctpStateTransportSctpStateThe new SCTP state.

Observer Events

See also Transport Observer Events.

webRtcTransport.observer.on(“icestatechange”, fn(iceState))

Same as the icestatechange event.

webRtcTransport.observer.on(“iceselectedtuplechange”, fn(iceSelectedTuple))

Same as the iceselectedtuplechange event.

webRtcTransport.observer.on(“dtlsstatechange”, fn(dtlsState))

Same as the dtlsstatechange event.

webRtcTransport.observer.on(“sctpstatechange”, fn(sctpState))

Same as the sctpstatechange event.

PlainRtpTransport (DEPRECATED)

PlainRtpTransport has been renamed to PlainTransport since mediasoup version 3.5.0.

PlainTransport

@inherits Transport

A plain transport represents a network path through which RTP, RTCP (optionally secured with SRTP) and SCTP (DataChannel) is transmitted.

Dictionaries

PlainTransportOptions

FieldTypeDescriptionRequiredDefault
listenIpTransportListenIp|StringListening IP address.Yes 
rtcpMuxBooleanUse RTCP-mux (RTP and RTCP in the same port).Notrue
comediaBooleanWhether remote IP:port should be auto-detected based on first RTP/RTCP packet received. If enabled, connect() must only be called if SRTP is enabled by providing the remote srtpParameters and nothing else.Nofalse
enableSctpBooleanCreate a SCTP association.Nofalse
numSctpStreamsNumSctpStreamsSCTP streams number.No 
maxSctpMessageSizeNumberMaximum allowed size for SCTP messages sent by DataProducers.No262144
sctpSendBufferSizeNumberSCTP send buffer size used by usrsctp.NO262144
enableSrtpBooleanEnable SRTP to encrypt RTP and SRTP. If enabled, the remote must also enable SRTP.Nofalse
srtpCryptoSuiteSrtpCryptoSuiteJust valid if enableSrtp is set.No“AES_CM_128_HMAC_SHA1_80”
appDataObjectCustom application data.No{ }
  • Note that comedia mode just makes sense when the remote endpoint is gonna produce RTP on this plain transport. Otherwise, if the remote endpoint does not send any RTP (or SCTP) packet to mediasoup, there is no way to detect its remote RTP IP and port, so the endpoint won't receive any packet from mediasoup.
  • In other words, do not use comedia mode if the remote endpoint is not going to produce RTP but just consume it. In those cases, do not set comedia flag and call connect() with the IP and port(s) of the remote endpoint.

Properties

See also Transport Properties.

plainTransport.tuple

The transport tuple. If RTCP-mux is enabled (rtcpMux is set), this tuple refers to both RTP and RTCP.

  • Once the plain transport is created, transport.tuple will contain information about its localIplocalPort and protocol.
  • Information about remoteIp and remotePort will be set:
    • after calling connect() method, or
    • via dynamic remote address detection when using comedia mode.

@type TransportTuple, read only

plainTransport.rtcpTuple

The transport tuple for RTCP. If RTCP-mux is enabled (rtcpMux is set), its value is undefined.

  • Once the plain transport is created (with RTCP-mux disabled), transport.rtcpTuple will contain information about its localIplocalPort and protocol.
  • Information about remoteIp and remotePort will be set:
    • after calling connect() method, or
    • via dynamic remote address detection when using comedia mode.

@type TransportTuple, read only

plainTransport.sctpParameters

Local SCTP parameters. Or undefined if SCTP is not enabled.

@type SctpParameters, read only

plainTransport.sctpState

Current SCTP state. Or undefined if SCTP is not enabled.

@type TransportSctpState, read only

plainTransport.srtpParameters

Local SRTP parameters representing the crypto suite and key material used to encrypt sending RTP and SRTP. Note that, if comedia mode is set, these local SRTP parameters may change after calling connect() with the remote SRTP parameters (to override the local SRTP crypto suite with the one given in connect()).

@type SrtpParameters, read only

Methods

See also Transport Methods.

plainTransport.getStats()

Returns current RTC statistics of the WebRTC transport.

@async

@override

@returns Array<PlainTransportStat>

Check the RTC Statistics section for more details.

plainTransport.connect({ ip, port, rtcpPort, srtpParameters })

Provides the plain transport with the endpoint parameters.

  • If comedia is enabled in this plain transport and SRTP is not, connect() must not be called.
  • If comedia is enabled and SRTP is also enabled (enableSrtp was set in the router.createPlainTransport() options) then connect() must be called with just the remote srtpParameters.
  • If comediap is disabled, connect() must be eventually called with remote ipport, optional rtcpPort (if RTCP-mux is not enabled) and optional srtpParameters (if SRTP is enabled).
ArgumentTypeDescriptionRequiredDefault
ipStringRemote IPv4 or IPv6. Required if comedia is not set.No 
portNumberRemote port. Required if comedia is not set.No 
rtcpPortNumberRemote RTCP port. Required if comedia is not set and RTCP-mux is not enabled.No 
srtpParametersSrtpParametersSRTP parameters used by the remote endpoint to encrypt its RTP and RTCP. The SRTP crypto suite of the local srtpParameters gets also updated after connect() resolves. Required if enableSrtp was set.No 

@async

@overrides

// Calling connect() on a PlainTransport created with comedia and rtcpMux set.
await plainTransport.connect(
  {
    ip   : '1.2.3.4',
    port : 9998
  });
// Calling connect() on a PlainTransport created with comedia unset and rtcpMux
// also unset.
await plainTransport.connect(
  {
    ip       : '1.2.3.4',
    port     : 9998,
    rtcpPort : 9999
  });
// Calling connect() on a PlainTransport created with comedia set and
// enableSrtp enabled.
await plainTransport.connect(
  {
    srtpParameters :
    {
      cryptoSuite : 'AES_CM_128_HMAC_SHA1_80',
      keyBase64   : 'ZnQ3eWJraDg0d3ZoYzM5cXN1Y2pnaHU5NWxrZTVv'
    }
  });
// Calling connect() on a PlainTransport created with comedia unset, rtcpMux
// set and enableSrtp enabled.
await plainTransport.connect(
  {
    ip             : '1.2.3.4',
    port           : 9998,
    srtpParameters :
    {
      cryptoSuite : 'AES_CM_128_HMAC_SHA1_80',
      keyBase64   : 'ZnQ3eWJraDg0d3ZoYzM5cXN1Y2pnaHU5NWxrZTVv'
    }
  });

Events

See also Transport Events.

plainTransport.on(“tuple”, fn(tuple))

Emitted after the remote RTP origin has been discovered. Just emitted if comedia mode was set.

ArgumentTypeDescription
tupleTransportTupleThe updated transport tuple.

plainTransport.on(“rtcptuple”, fn(rtcpTuple))

Emitted after the remote RTCP origin has been discovered. Just emitted if comedia mode was set and rtcpMux was not.

ArgumentTypeDescription
rtcpTupleTransportTupleThe updated RTCP transport tuple.

plainTransport.on(“sctpstatechange”, fn(sctpState))

Emitted when the transport SCTP state changes.

ArgumentTypeDescription
sctpStateTransportSctpStateThe new SCTP state.

Observer Events

See also Transport Observer Events.

plainTransport.observer.on(“tuple”, fn(tuple))

Same as the tuple event.

plainTransport.observer.on(“rtcptuple”, fn(rtcpTuple))

Same as the rtcpTuple event.

plainTransport.observer.on(“sctpstatechange”, fn(sctpState))

Same as the sctpstatechange event.

PipeTransport

@inherits Transport

A pipe transport represents a network path through which RTP, RTCP (optionally secured with SRTP) and SCTP (DataChannel) is transmitted. Pipe transports are intented to intercommunicate two Router instances collocated on the same host or on separate hosts.

When calling consume() on a pipe transport, all RTP streams of the Producer are transmitted verbatim (in contrast to what happens in WebRtcTransport and PlainTransport in which a single and continuos RTP stream is sent to the consuming endpoint).

Dictionaries

PipeTransportOptions

FieldTypeDescriptionRequiredDefault
listenIpTransportListenIp|StringListening IP address.Yes 
enableSctpBooleanCreate a SCTP association.Nofalse
numSctpStreamsNumSctpStreamsSCTP streams number.No 
maxSctpMessageSizeNumberMaximum allowed size for SCTP messages sent by DataProducers.No268435456
sctpSendBufferSizeNumberSCTP send buffer size used by usrsctp.NO268435456
enableRtxBooleanEnable RTX and NACK for RTP retransmission. Useful if both pipeTransports run in different hosts. If enabled, the paired pipeTransport must also enable this setting.Nofalse
enableSrtpBooleanEnable SRTP to encrypt RTP and SRTP. If enabled, the paired pipeTransport must also enable this setting.Nofalse
appDataObjectCustom application data.No{ }

Properties

See also Transport Properties.

pipeTransport.tuple

The transport tuple. It refers to both RTP and RTCP since pipe transports use RTCP-mux by design.

  • Once the pipe transport is created, transport.tuple will contain information about its localIplocalPort and protocol.
  • Information about remoteIp and remotePort will be set after calling connect() method.

@type TransportTuple, read only

pipeTransport.sctpParameters

Local SCTP parameters. Or undefined if SCTP is not enabled.

@type SctpParameters, read only

pipeTransport.sctpState

Current SCTP state. Or undefined if SCTP is not enabled.

@type TransportSctpState, read only

pipeTransport.srtpParameters

Local SRTP parameters representing the crypto suite and key material used to encrypt sending RTP and SRTP. Those parameters must be given to the paired pipeTransport in the connect() method.

@type SrtpParameters, read only

Methods

See also Transport Methods.

pipeTransport.getStats()

Returns current RTC statistics of the pipe transport.

@async

@override

@returns Array<PipeTransportStat>

Check the RTC Statistics section for more details.

pipeTransport.connect({ ip, port })

Provides the pipe RTP transport with the remote parameters.

ArgumentTypeDescriptionRequiredDefault
ipStringRemote IPv4 or IPv6.Yes 
portNumberRemote port.Yes 
srtpParametersSrtpParametersSRTP parameters used by the paired pipeTransport to encrypt its RTP and RTCP.No 

@async

@overrides

await pipeTransport.connect(
  {
    ip             : '1.2.3.4',
    port           : 9999,
    srtpParameters :
    {
      cryptoSuite : 'AES_CM_128_HMAC_SHA1_80',
      keyBase64   : 'ZnQ3eWJraDg0d3ZoYzM5cXN1Y2pnaHU5NWxrZTVv'
    }
  });

Events

See also Transport Events.

pipeTransport.on(“sctpstatechange”, fn(sctpState))

Emitted when the transport SCTP state changes.

ArgumentTypeDescription
sctpStateTransportSctpStateThe new SCTP state.

Observer Events

See also Transport Observer Events.

pipeTransport.observer.on(“sctpstatechange”, fn(sctpState))

Same as the sctpstatechange event.

DirectTransport

@inherits Transport

A direct transport represents a direct connection between the mediasoup Node.js process and a Router instance in a mediasoup-worker subprocess.

A direct transport can be used to directly send and receive data messages from/to Node.js by means of DataProducers and DataConsumers of type 'direct' created on a direct transport. Direct messages sent by a DataProducer in a direct transport can be consumed by endpoints connected through a SCTP capable transport (WebRtcTransportPlainTransportPipeTransport) and also by the Node.js application by means of a DataConsumer created on a DirectTransport (and vice-versa: messages sent over SCTP/DataChannel can be consumed by the Node.js application by means of a DataConsumer created on a DirectTransport).

A direct transport can also be used to inject and directly consume RTP and RTCP packets in Node.js by using the producer.send(rtpPacket) and consumer.on('rtp') API (plus directTransport.sendRtcp(rtcpPacket) and directTransport.on('rtcp') API).

Dictionaries

DirectTransportOptions

FieldTypeDescriptionRequiredDefault
maxMessageSizeNumberMaximum allowed size for direct messages sent by DataProducers.No262144
appDataObjectCustom application data.No{ }

Properties

See also Transport Properties.

Methods

See also Transport Methods.

directTransport.getStats()

Returns current RTC statistics of the direct transport.

@async

@override

@returns Array<DirectTransportStat>

Check the RTC Statistics section for more details.

directTransport.connect()

It's a no-op. There is no need to call this method on direct transports (they are always connected).

@async

@overrides

directTransport.setMaxIncomingBitrate(options)

Not implemented in direct transports. If called, it will reject with UnsupportedError.

@async

@overrides

directTransport.sendRtcp(rtcpPacket)

Sends a RTCP packet from the Node.js process.

Just available in direct transports, this is, those created via router.createDirectTransport().

ArgumentTypeDescriptionRequiredDefault
rtcpPacketBufferA Node.js Buffer containing a valid RTCP packet (can be a compound packet).Yes 
// Send a RTCP packet.
directTransport.sendRtcp(rtcpPacket);

</section>

Events

See also Transport Events.

directTransport.on(“rtcp”, fn(rtcpPacket))

Emitted when the direct transport receives a RTCP packet from its router.

Just available in direct transports, this is, those created via router.createDirectTransport().

ArgumentTypeDescription
rtcpPacketBufferReceived RTP packet. It's always a Node.js Buffer. It may be a compound RTCP packet or a standalone RTCP packet.
directTransport.on("rtcp", (rtcpPacket) =>
{
  // Do stuff with the binary RTCP packet.
});

Observer Events

See also Transport Observer Events.

Producer

A producer represents an audio or video source being injected into a mediasoup router. It's created on top of a transport that defines how the media packets are carried.

Dictionaries

ProducerOptions

FieldTypeDescriptionRequiredDefault
kindMediaKindMedia kind (“audio” or “video”).Yes 
rtpParametersRtpSendParametersRTP parameters defining what the endpoint is sending.Yes 
pausedBooleanWhether the producer must start in paused mode.Nofalse
keyFrameRequestDelayNumberJust for video. Time (in ms) before asking the sender for a new key frame after having asked a previous one. If 0 there is no delay.No0
appDataObjectCustom application data.No{ }

Check the RTP Parameters and Capabilities section for more details.

ProducerScore

FieldTypeDescriptionRequiredDefault
encodingIdxNumberIndex of the RTP stream in the rtpParameters.encodings array of the producer.Yes 
ssrcNumberRTP stream SSRC.Yes 
ridStringRTP stream RID value.No 
scoreNumberRTP stream score (from 0 to 10) representing the transmission quality.Yes 

ProducerVideoOrientation

As documented in WebRTC Video Processing and Codec Requirements.

FieldTypeDescriptionRequiredDefault
cameraBooleanWhether the source is a video camera.Yes 
flipBooleanWhether the video source is flipped.Yes 
rotationNumberRotation degrees (0, 90, 180 or 270).Yes 

ProducerTraceEventData

FieldTypeDescriptionRequiredDefault
typeProducerTraceEventTypeTrace event type.Yes 
timestampNumberEvent timestamp.Yes 
directionString“in” (icoming direction) or “out” (outgoing direction).Yes 
infoObjectPer type specific information.Yes 

See also “trace” Event in the Debugging section.

Enums

ProducerType

ValueDescription
“simple”A single RTP stream is received with no spatial/temporal layers.
“simulcast”Two or more RTP streams are received, each of them with one or more temporal layers.
“svc”A single RTP stream is received with spatial/temporal layers.

ProducerTraceEventType

ValueDescription
“rtp”RTP packet.
“keyframe”RTP video keyframe packet.
“nack”RTCP NACK packet.
“pli”RTCP PLI packet.
“fir”RTCP FIR packet.

Properties

producer.id

Producer identifier.

@type String, read only

producer.closed

Whether the producer is closed.

@type Boolean, read only

producer.kind

The media kind (“audio” or “video”).

@type MediaKind, read only

producer.rtpParameters

Producer RTP parameters.

@type RtpSendParameters, read only

Check the RTP Parameters and Capabilities section for more details.

producer.type

Producer type.

@type ProducerType, read only

producer.paused

Whether the producer is paused.

@type Boolean, read only

producer.score

The score of each RTP stream being received, representing their tranmission quality.

@type Array<ProducerScore>, read only

producer.appData

Custom data Object provided by the application in the producer factory method. The app can modify its content at any time.

@type Object, read only

producer.observer

See the Observer Events section below.

@type EventEmitter, read only

Methods

producer.close()

Closes the producer. Triggers a “producerclose” event in all its associated consumers.

producer.getStats()

Returns current RTC statistics of the producer.

@async

@returns Array<ProducerStat>

Check the RTC Statistics section for more details.

producer.pause()

Pauses the producer (no RTP is sent to its associated consumers). Triggers a “producerpause” event in all its associated consumers.

@async

producer.resume()

Resumes the producer (RTP is sent again to its associated consumers). Triggers a “producerresume” event in all its associated consumers.

@async

producer.enableTraceEvent(types)

Instructs the producer to emit “trace” events. For monitoring purposes. Use with caution.

ArgumentTypeDescriptionRequiredDefault
typesArray<ProducerTraceEventDataEventType>Enabled types.NoUnset (so disabled)

@async

await producer.enableTraceEvent([ "rtp", "pli" ]);

producer.on("trace", (trace) =>
{
  // trace.type can be "rtp" or "pli".
});

producer.send(rtpPacket)

Sends a RTP packet from the Node.js process.

Just available in direct transports, this is, those created via router.createDirectTransport().

ArgumentTypeDescriptionRequiredDefault
rtpPacketBufferA Node.js Buffer containing a valid RTP packet (according to the RtpParameters of the producer).Yes 
const producer = await directTransport.produce(
  {
    kind          : "audio", 
    rtpParameters : { ... },
  });

// Send a RTP packet.
producer.send(rtpPacket);

Events

producer.on(“transportclose”, fn())

Emitted when the transport this producer belongs to is closed for whatever reason. The producer itself is also closed. A “producerclose” event is triggered in all its associated consumers.

producer.on("transportclose", () =>
{
  console.log("transport closed so producer closed");
});

producer.on(“score”, fn(score))

Emitted when the producer score changes.

ArgumentTypeDescription
scoreArray<ProducerScore>RTP streams' scores.

producer.on(“videoorientationchange”, fn(videoOrientation))

Emitted when the video orientation changes. This is just possible if the “urn:3gpp:video-orientation” RTP extension has been negotiated in the producer RTP parameters.

ArgumentTypeDescription
videoOrientationProducerVideoOrientationNew video orientation.

producer.on(“trace”, fn(trace))

See enableTraceEvent() method.

ArgumentTypeDescription
traceProducerTraceEventDataTrace data.
producer.on("trace", (trace) =>
{
  console.log(trace);
});

Observer Events

See the Observer API section below.

producer.observer.on(“close”, fn())

Emitted when the producer is closed for whatever reason.

producer.observer.on(“pause”, fn())

Emitted when the producer is paused.

producer.observer.on(“resume”, fn())

Emitted when the producer is resumed.

producer.observer.on(“score”, fn(score))

Same as the score event.

producer.observer.on(“videoorientationchange”, fn(videoOrientation))

Same as the videoorientationchange event.

producer.observer.on(“trace”, fn(trace))

Same as the trace event.

Consumer

A consumer represents an audio or video source being forwarded from a mediasoup router to an endpoint. It's created on top of a transport that defines how the media packets are carried.

Dictionaries

ConsumerOptions

FieldTypeDescriptionRequiredDefault
producerIdStringThe id of the producer to consume.Yes 
rtpCapabilitiesRtpCapabilitiesRTP capabilities of the consuming endpoint.Yes 
pausedBooleanWhether the consumer must start in paused mode. See note below.Nofalse
preferredLayersConsumerLayersPreferred spatial and temporal layer for simulcast or SVC media sources. If unset, the highest ones are selected.No 
pipeBooleanWhether this consumer should consume all RTP streams generated by the producer instead of consuming a single and continuos RTP stream (same behavior as when consuming in a pipe transport, in which this setting is always implicit).Nofalse
appDataObjectCustom application data.No{ }

Check the RTP Parameters and Capabilities section for more details.

ConsumerLayers

FieldTypeDescriptionRequiredDefault
spatialLayerNumberThe spatial layer index (from 0 to N).Yes 
temporalLayerNumberThe temporal layer index (from 0 to N).No 

ConsumerScore

FieldTypeDescriptionRequiredDefault
scoreNumberScore of the RTP stream in the consumer (from 0 to 10) representing its transmission quality.Yes 
producerScoreNumberScore of the currently selected RTP stream in the associated producer (from 0 to 10) representing its transmission quality.Yes 
producerScoresArray<Number>The scores of all RTP streams in the producer ordered by encoding (just useful when the producer uses simulcast).Yes 

ConsumerTraceEventData

FieldTypeDescriptionRequiredDefault
typeConsumerTraceEventTypeTrace event type.Yes 
timestampNumberEvent timestamp.Yes 
directionString“in” (icoming direction) or “out” (outgoing direction).Yes 
infoObjectPer type specific information.Yes 

See also “trace” Event in the Debugging section.

Enums

ConsumerType

ValueDescription
“simple”A single RTP stream is sent with no spatial/temporal layers.
“simulcast”Two or more RTP streams are sent, each of them with one or more temporal layers.
“svc”A single RTP stream is sent with spatial/temporal layers.
“pipe”Special type for consumers created on a PipeTransport.

ConsumerTraceEventType

ValueDescription
“rtp”RTP packet.
“keyframe”RTP video keyframe packet.
“nack”RTCP NACK packet.
“pli”RTCP PLI packet.
“fir”RTCP FIR packet.

Properties

consumer.id

Consumer identifier.

@type String, read only

consumer.producerId

The associated producer identifier.

@type String, read only

consumer.closed

Whether the consumer is closed.

consumer.kind

The media kind (“audio” or “video”).

@type MediaKind, read only

consumer.rtpParameters

Consumer RTP parameters.

@type RtpReceiveParameters, read only

Check the RTP Parameters and Capabilities section for more details.

consumer.type

Consumer type.

@type ConsumerType, read only

consumer.paused

Whether the consumer is paused. It does not take into account whether the associated producer is paused.

@type Boolean, read only

consumer.producerPaused

Whether the associated producer is paused.

@type Boolean, read only

consumer.score

The score of the RTP stream being sent, representing its tranmission quality.

@type ConsumerScore, read only

consumer.preferredLayers

Preferred spatial and temporal layers (see setPreferredLayers() method). For simulcast and SVC consumers, undefined otherwise.

@type ConsumerLayers|Undefined, read only

consumer.currentLayers

Currently active spatial and temporal layers (for simulcast and SVC consumers only). It's undefined if no layers are being sent to the consuming endpoint at this time (or if the consumer is consuming from a simulcast or svc producer).

@type ConsumerLayers|Undefined, read only

consumer.priority

Consumer priority (see setPriority() method).

@type Number, read only

consumer.appData

Custom data Object provided by the application in the consumer factory method. The app can modify its content at any time.

@type Object, read only

consumer.observer

See the Observer Events section below.

@type EventEmitter, read only

Methods

consumer.close()

Closes the consumer.

consumer.getStats()

Returns current RTC statistics of the consumer.

@async

@returns Array<ConsumerStat>

Check the RTC Statistics section for more details.

consumer.pause()

Pauses the consumer (no RTP is sent to the consuming endpoint).

@async

consumer.resume()

Resumes the consumer (RTP is sent again to the consuming endpoint).

@async

consumer.setPreferredLayers(preferredLayers)

Sets the preferred (highest) spatial and temporal layers to be sent to the consuming endpoint. Just valid for simulcast and SVC consumers.

ArgumentTypeDescriptionRequiredDefault
preferredLayersConsumerLayersPreferred spatial and temporal layers. The temporal layer is optional (if unset, the highest one is chosen).Yes 

@async

await consumer.setPreferredLayers({ spatialLayer: 3 });

consumer.setPriority(priority)

Sets the priority for this consumer. It affects how the estimated outgoing bitrate in the transport (obtained via transport-cc or REMB) is distributed among all video consumers, by priorizing those with higher priority.

ArgumentTypeDescriptionRequiredDefault
priorityNumberFrom 1 (minimum) to 255 (maximum).Yes 

@async

Consumers' priority is only appreciable when there is not enough estimated outgoing bitrate to satisfy the needs of all video consumers.

await consumer.setPriority(2);

consumer.unsetPriority()

Unsets the priority for this consumer (it sets it to its default value 1).

@async

await consumer.unsetPriority();

consumer.requestKeyFrame()

Request a key frame to the associated producer. Just valid for video consumers.

@async

consumer.enableTraceEvent(types)

Instructs the consumer to emit “trace” events. For monitoring purposes. Use with caution.

ArgumentTypeDescriptionRequiredDefault
typesArray<ConsumerTraceEventType>Enabled types.NoUnset (so disabled)

@async

await consumer.enableTraceEvent([ "rtp", "pli", "fir" ]);

consumer.on("trace", (trace) =>
{
  // trace.type can be "rtp" or "pli" or "fir".
});

Events

consumer.on(“transportclose”, fn())

Emitted when the transport this consumer belongs to is closed for whatever reason. The consumer itself is also closed.

consumer.on("transportclose", () =>
{
  console.log("transport closed so consumer closed");
});

consumer.on(“producerclose”, fn())

Emitted when the associated producer is closed for whatever reason. The consumer itself is also closed.

consumer.on("producerclose", () =>
{
  console.log("associated producer closed so consumer closed");
});

consumer.on(“producerpause”, fn())

Emitted when the associated producer is paused.

consumer.on(“producerresume”, fn())

Emitted when the associated producer is resumed.

consumer.on(“score”, fn(score))

Emitted when the consumer score changes.

ArgumentTypeDescription
scoreConsumerScoreRTP stream score.

consumer.on(“layerschange”, fn(layers))

Emitted when the spatial/temporal layers being sent to the endpoint change. Just for simulcast or SVC consumers.

ArgumentTypeDescription
layersConsumerLayers|UndefinedCurrent spatial and temporal layers (or undefined if there are no current layers).

This event is emitted under various circumstances in SVC or simulcast consumers (assuming the consumer endpoints supports BWE via REMB or Transport-CC):

  • When the consumer (or its associated producer) is paused.
  • When all the RTP streams of the associated producer become inactive (no RTP received for a while).
  • When the available bitrate of the BWE makes the consumer upgrade or downgrade the spatial and/or temporal layers.
  • When there is no available bitrate for this consumer (even for the lowest layers) so the event fires with null as argument.

The Node.js application can detect the latter (consumer deactivated due to not enough bandwidth) by checking if both consumer.paused and consumer.producerPaused are falsy after the consumer has emitted this event with null as argument.

consumer.on(“trace”, fn(trace))

See enableTraceEvent() method.

ArgumentTypeDescription
traceConsumerTraceEventDataTrace data.
consumer.on("trace", (trace) =>
{
  console.log(trace);
});

consumer.on(“rtp”, fn(rtpPacket))

Emitted when the consumer receives through its router a RTP packet from the associated producer.

Just available in direct transports, this is, those created via router.createDirectTransport().

ArgumentTypeDescription
rtpPacketBufferReceived RTP packet. It's always a Node.js Buffer.
consumer.on("rtp", (rtpPacket) =>
{
  // Do stuff with the binary RTP packet.
});

Observer Events

See the Observer API section below.

consumer.observer.on(“close”, fn())

Emitted when the consumer is closed for whatever reason.

consumer.observer.on(“pause”, fn())

Emitted when the consumer or its associated producer is paused and, as result, the consumer becomes paused.

consumer.observer.on(“resume”, fn())

Emitted when the consumer or its associated producer is resumed and, as result, the consumer is no longer paused.

consumer.observer.on(“score”, fn(score))

Same as the score event.

consumer.observer.on(“layerschange”, fn(layers))

Same as the layerschange event.

consumer.observer.on(“trace”, fn(trace))

Same as the trace event.

DataProducer

A data producer represents an endpoint capable of injecting data messages into a mediasoup Router. A data producer can use SCTP (AKA DataChannel) to deliver those messages, or can directly send them from the Node.js application if the data producer was created on top of a DirectTransport.

Dictionaries

DataProducerOptions

FieldTypeDescriptionRequiredDefault
sctpStreamParametersSctpStreamParametersSCTP parameters defining how the endpoint is sending the data. Required if SCTP/DataChannel is used. Must not be given if the data producer is created on a DirectTransport.No 
labelStringA label which can be used to distinguish this DataChannel from others.No 
protocolStringName of the sub-protocol used by this DataChannel.No 
appDataObjectCustom application data.No{ }

Enums

DataProducerType

ValueDescription
“sctp”The endpoint sends messages using the SCTP protocol.
“direct”Messages are sent directly from the Node.js process over a direct transport.

Properties

dataProducer.id

Data producer identifier.

@type String, read only

dataProducer.closed

Whether the data producer is closed.

@type Boolean, read only

dataProducer.type

The type of the data producer.

@type DataProducerType, read only

dataProducer.sctpStreamParameters

The SCTP stream parameters (just if the data producer type is 'sctp').

@type SctpStreamParameters|Undefined, read only

dataProducer.label

The data producer label.

@type String , read only

dataProducer.protocol

The data producer sub-protocol.

@type String , read only

dataProducer.appData

Custom data Object provided by the application in the producer factory method. The app can modify its content at any time.

@type Object, read only

dataProducer.observer

See the Observer Events section below.

@type EventEmitter, read only

Methods

dataProducer.close()

Closes the producer. Triggers a “dataproducerclose” event in all its associated consumers.

dataProducer.getStats()

Returns current statistics of the data producer.

@async

@returns Array<DataProducerStat>

Check the RTC Statistics section for more details.

dataProducer.send(message, ppid)

Sends direct messages from the Node.js process.

Just available in direct transports, this is, those created via router.createDirectTransport().

ArgumentTypeDescriptionRequiredDefault
messageString|BufferMessage to be sent (can be binary by using a Node.js Buffer).Yes 
ppidNumberMimics the SCTP Payload Protocol Identifier. In most cases it must not be set.No51 (WebRTC String) if message is a String and 53 (WebRTC Binary) if it's a Buffer.
const stringMessage = "hello";
const binaryMessage = Buffer.from([ 1, 2, 3, 4 ]);

dataProducer.send(stringMessage);
dataProducer.send(binaryMessage);

Events

dataProducer.on(“transportclose”, fn())

Emitted when the transport this data producer belongs to is closed for whatever reason. The producer itself is also closed. A “dataproducerclose” event is triggered in all its associated consumers.

dataProducer.on("transportclose", () =>
{
  console.log("transport closed so dataProducer closed");
});

Observer Events

See the Observer API section below.

dataProducer.observer.on(“close”, fn())

Emitted when the producer is closed for whatever reason.

DataConsumer

A data copnsumer represents an endpoint capable of receiving data messages from a mediasoup Router. A data consumer can use SCTP (AKA DataChannel) to receive those messages, or can directly receive them in the Node.js application if the data consumer was created on top of a DirectTransport.

Dictionaries

DataConsumerOptions

FieldTypeDescriptionRequiredDefault
dataProducerIdStringThe id of the data producer to consume.Yes 
orderedBooleanJust if consuming over SCTP. Whether data messages must be received in order. If true the messages will be sent reliably.NoThe value in the data producer (if it's of type 'sctp') or true (if it's of type 'direct').
maxPacketLifeTimeNumberJust if consuming over SCTP. When ordered is false, it indicates the time (in milliseconds) after which a SCTP packet will stop being retransmitted.NoThe value in the data producer (if it's of type 'sctp') or unset (if it's of type 'direct').
maxRetransmitsNumberJust if consuming over SCTP. When ordered is false, it indicates the maximum number of times a packet will be retransmitted.NoThe value in the data producer (if it's of type 'sctp') or unset (if it's of type 'direct').
appDataObjectCustom application data.No{ }

Enums

DataConsumerType

ValueDescription
“sctp”The endpoint receives messages using the SCTP protocol.
“direct”Messages are received directly by the Node.js process over a direct transport.

Properties

dataConsumer.id

Data consumer identifier.

@type String, read only

dataConsumer.dataProducerId

The associated data producer identifier.

@type String, read only

dataConsumer.closed

Whether the data consumer is closed.

@type Boolean, read only

dataConsumer.type

The type of the data consumer.

@type DataProducerType, read only

dataConsumer.sctpStreamParameters

The SCTP stream parameters (just if the data consumer type is 'sctp').

@type SctpStreamParameters|Undefined, read only

dataConsumer.label

The data consumer label.

@type String , read only

dataConsumer.protocol

The data consumer sub-protocol.

@type String , read only

dataConsumer.appData

Custom data Object provided by the application in the data consumer factory method. The app can modify its content at any time.

@type Object, read only

dataConsumer.observer

See the Observer Events section below.

@type EventEmitter, read only

Methods

dataConsumer.close()

Closes the data consumer.

dataConsumer.getStats()

Returns current statistics of the data consumer.

@async

@returns Array<DataConsumerStat>

Check the RTC Statistics section for more details.

dataConsumer.getBufferedAmount()

Returns the number of bytes of data currently buffered to be sent over the underlaying SCTP association.

The underlaying SCTP association uses a common send buffer for all data consumers, hence the value given by this method indicates the data buffered for all data consumers in the transport.

@async

@returns Number;

dataConsumer.setBufferedAmountLowThreshold()

FieldTypeDescriptionRequiredDefault
bufferedAmountLowThresholdNumberBytes of buffered outgoing data that is considered low.No0

Whenever the underlaying SCTP association buffered bytes drop to this value, bufferedamountlow event is fired.

@async

dataConsumer.send(message, ppid)

Sends direct messages from the Node.js process.

Just available in data consumers of type “SCTP”.

ArgumentTypeDescriptionRequiredDefault
messageString|BufferMessage to be sent (can be binary by using a Node.js Buffer).Yes 
ppidNumberMimics the SCTP Payload Protocol Identifier. In most cases it must not be set.No51 (WebRTC String) if message is a String and 53 (WebRTC Binary) if it's a Buffer.
const stringMessage = "hello";
const binaryMessage = Buffer.from([ 1, 2, 3, 4 ]);

dataConsumer.send(stringMessage);
dataConsumer.send(binaryMessage);

@async

Events

dataConsumer.on(“transportclose”, fn())

Emitted when the transport this data consumer belongs to is closed for whatever reason. The data consumer itself is also closed.

dataConsumer.on("transportclose", () =>
{
  console.log("transport closed so dataConsumer closed");
});

dataConsumer.on(“dataproducerclose”, fn())

Emitted when the associated data producer is closed for whatever reason. The data consumer itself is also closed.

dataConsumer.on("dataproducerclose", () =>
{
  console.log("associated data producer closed so dataConsumer closed");
});

dataConsumer.on(“message”, fn(message, ppid))

Emitted when a message has been received from the corresponding data producer,

Just available in direct transports, this is, those created via router.createDirectTransport().

ArgumentTypeDescription
messageBufferReceived message. It's always a Node.js Buffer.
ppidNumberMimics the SCTP Payload Protocol Identifier. Typically it's 51 (WebRTC String) if message is a String and 53 (WebRTC Binary) if it's a Buffer.
dataConsumer.on("message", (message, ppid) =>
{
  if (ppid === 51)
    console.log("text message received:", message.toString("utf-8"));
  else if (ppid === 53)
    console.log("binary message received");
});

dataConsumer.on(“sctpsendbufferfull”)

Emitted when a message could not be sent because the SCTP send buffer was full.

dataConsumer.on(“bufferedamountlow”, fn(bufferedAmount))

Emitted when the underlaying SCTP association buffered bytes drop down to bufferedAmountLowThreshold.

ArgumentTypeDescription
bufferedAmountNumberNumber of bytes buffered in the underlaying SCTP association.

Only applicable for consumers of type 'sctp'.

Observer Events

See the Observer API section below.

dataConsumer.observer.on(“close”, fn())

Emitted when the data consumer is closed for whatever reason.

RtpObserver

@abstract

An RTP observer inspects the media received by a set of selected producers.

mediasoup implements the following RTP observer classes:

Dictionaries

RtpObserverAddRemoveProducerOptions

FieldTypeDescriptionRequiredDefault
producerIdStringId of the producer to add or remove.Yes 

Properties

These are properties common to all RTP observer classes. Each RTP observer class may define new ones.

rtpObserver.id

RTP observer identifier.

@type String, read only

rtpObserver.closed

Whether the RTP observer is closed.

@type Boolean, read only

rtpObserver.paused

Whether the RTP observer is paused.

@type Boolean, read only

rtpObserver.appData

Custom data Object provided by the application in the RTP observer factory method. The app can modify its content at any time.

@type Object, read only

rtpObserver.observer

See the Observer Events section below.

@type EventEmitter, read only

Methods

These are methods common to all RTP observer classes. Each RTP observer class may define new ones.

rtpObserver.close()

Closes the RTP observer.

rtpObserver.pause()

Pauses the RTP observer. No RTP is inspected until resume() is called.

@async

rtpObserver.resume()

Resumes the RTP observer. RTP is inspected again.

@async

rtpObserver.addProducer(options)

Provides the RTP observer with a new producer to monitor.

ArgumentTypeDescriptionRequiredDefault
optionsRtpObserverAddRemoveProducerOptionsOptions.Yes 

@async

rtpObserver.removeProducer(options)

Removes the given producer from the RTP observer.

ArgumentTypeDescriptionRequiredDefault
optionsRtpObserverAddRemoveProducerOptionsOptions.Yes 

@async

Events

These are events common to all RTP observer classes. Each RTP observer class may define new ones.

rtpObserver.on(“routerclose”)

Emitted when the router this RTP observer belongs to is closed for whatever reason. The RTP observer itself is also closed.

rtpObserver.on("routerclose", () =>
{
  console.log("router closed so RTP observer closed");
});

Observer Events

See the Observer API section below.

These are observer events common to all RTP observer classes. Each transport class may define new ones.

rtpObserver.observer.on(“close”, fn())

Emitted when the RTP observer is closed for whatever reason.

rtpObserver.observer.on(“pause”, fn())

Emitted when the RTP observer is paused.

rtpObserver.observer.on(“resume”, fn())

Emitted when the RTP observer is resumed.

rtpObserver.observer.on(“addproducer”, fn(producer))

Emitted when a new producer is added into the RTP observer.

ArgumentTypeDescription
producerProducerNew producer.

rtpObserver.observer.on(“removeproducer”, fn(producer))

Emitted when a producer is removed from the RTP observer.

ArgumentTypeDescription
producerProducerNew producer.

AudioLevelObserver

@inherits RtpObserver

An audio level observer monitors the volume of the selected audio producers. It just handles audio producers (if addProducer() is called with a video producer it will fail).

Audio levels are read from an RTP header extension. No decoding of audio data is done. See RFC6464 for more information.

Dictionaries

AudioLevelObserverOptions

FieldTypeDescriptionRequiredDefault
maxEntriesNumberMaximum number of entries in the “volumes” event.No1
thresholdNumberMinimum average volume (in dBvo from -127 to 0) for entries in the “volumes” event.No-80
intervalNumberInterval in ms for checking audio volumes.No1000
appDataObjectCustom application data.No{ }

AudioLevelObserverVolume

FieldTypeDescriptionRequiredDefault
producerProducerThe audio producer instance.Yes 
volumeNumberThe average volume (in dBvo from -127 to 0) of the audio producer in the last interval.Yes 

Properties

See also RtpObserver Properties.

Methods

See also RtpObserver Methods.

Events

See also RtpObserver Events.

audioLevelObserver.on(“volumes”, fn(volumes))

Emitted at most every interval ms (see AudioLevelObserverOptions).

ArgumentTypeDescription
volumesArray<AudioLevelObserverVolume>Audio volumes entries ordered by volume (louder ones go first).

audioLevelObserver.on(“silence”)

Emitted when no one of the producers in this RTP observer is generating audio with a volume beyond the given threshold.

Observer Events

See also RTP Observer Observer Events.

audioLevelObserver.observer.on(“volumes”, fn(volumes))

Same as the volumes event.

audioLevelObserver.observer.on(“silence”)

Same as the silence event.

Observer API

Most entities in mediasoup expose a observer property (a Node.js EventEmitter) that can be used by third party libraries to monitor everything related to mediasoup.

The observer API should not be directly used by the application itself, but by separate modules or libraries that the application integrate into its code. Such a module or library may, for example, monitor all the creation and closure of workers, routers, transports, etc. It could also monitor events generated by producers and consumers (“pause”, “resume”, “score”, “layerschange”, etc).

Usage example:

const mediasoup = require("mediasoup");

mediasoup.observer.on("newworker", (worker) =>
{
  console.log("new worker created [worke.pid:%d]", worker.pid);

  worker.observer.on("close", () => 
  {
    console.log("worker closed [worker.pid:%d]", worker.pid);
  });

  worker.observer.on("newrouter", (router) =>
  {
    console.log(
      "new router created [worker.pid:%d, router.id:%s]",
      worker.pid, router.id);

    router.observer.on("close", () => 
    {
      console.log("router closed [router.id:%s]", router.id);
    });

    router.observer.on("newtransport", (transport) =>
    {
      console.log(
        "new transport created [worker.pid:%d, router.id:%s, transport.id:%s]",
        worker.pid, router.id, transport.id);

      transport.observer.on("close", () => 
      {
        console.log("transport closed [transport.id:%s]", transport.id);
      });

      transport.observer.on("newproducer", (producer) =>
      {
        console.log(
          "new producer created [worker.pid:%d, router.id:%s, transport.id:%s, producer.id:%s]",
          worker.pid, router.id, transport.id, producer.id);

        producer.observer.on("close", () => 
        {
          console.log("producer closed [producer.id:%s]", producer.id);
        });
      });

      transport.observer.on("newconsumer", (consumer) =>
      {
        console.log(
          "new consumer created [worker.pid:%d, router.id:%s, transport.id:%s, consumer.id:%s]",
          worker.pid, router.id, transport.id, consumer.id);

        consumer.observer.on("close", () => 
        {
          console.log("consumer closed [consumer.id:%s]", consumer.id);
        });
      });

      transport.observer.on("newdataproducer", (dataProducer) =>
      {
        console.log(
          "new data producer created [worker.pid:%d, router.id:%s, transport.id:%s, dataProducer.id:%s]",
          worker.pid, router.id, transport.id, dataProducer.id);

        dataProducer.observer.on("close", () => 
        {
          console.log("data producer closed [dataProducer.id:%s]", dataProducer.id);
        });
      });

      transport.observer.on("newdataconsumer", (dataConsumer) =>
      {
        console.log(
          "new data consumer created [worker.pid:%d, router.id:%s, transport.id:%s, dataConsumer.id:%s]",
          worker.pid, router.id, transport.id, dataConsumer.id);

        dataConsumer.observer.on("close", () => 
        {
          console.log("data consumer closed [dataConsumer.id:%s]", dataConsumer.id);
        });
      });
    });
  });
});
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值