Skip to contentSkip to navigationSkip to topbar
Page tools
Useful for sharing or LLM
Accelerate development with AI

On this page
Looking for more inspiration?Visit the

Node.js Media SDK best practices


To help you build reliable server-side media apps with the Video Media SDK for Node.js, review these best practices on pacing frames, managing resources, and tuning delivery.


Pace frames and avoid back pressure

pace-frames-and-avoid-back-pressure page anchor

Push frames at their real frame rate and keep timestamps moving forward.

To avoid queuing faster than the Room can accept, check the return values of LocalVideoTrack.write() and LocalAudioTrack.write(). When the underlying source isn't ready, these methods return false.

Audio has extreme time-sensitivity. If you write frames on a plain setInterval, playback might drift and click. Pace the playback using a drift-compensated writer that drains a buffer queue at exactly 48 kHz.

To review a working example, see audio_push.js(link takes you to an external page) and its helpers/paced-audio-writer.js(link takes you to an external page) in the SDK repository(link takes you to an external page).


Manage memory around frame buffers

manage-memory-around-frame-buffers page anchor

A busy Room produces many large video frames.

  • Avoid retaining any frame Buffer past the onFrame() callback.
  • Where you can, reuse allocations. If you retain references to old frames, it keeps them in memory and increases garbage-collection pressure.

Write checks for events about the connection state.

  • To track connection state, listen for the reconnecting and reconnected events.
  • To release resources, listen for the disconnected events.

When you finish with a track or a subscription:

  • Stop receiving frames with the removeFrameCallback() method.
  • Unpublish unneeded local tracks with the LocalTrackPublication.unpublish() method.
  • Close the connection to the Room with the room.disconnect() method.
  • Stop any of your ongoing push loops.

While developing with setLogLevel(), set the native log level. This method accepts a level name from off through all.

  • To handle failures, inspect the TwilioError passed to the disconnected and connectFailure events, and branch on its subclass, such as AccessTokenInvalidError, RoomNotFoundError, or SignalingConnectionError.
  • The ErrorCode enum lists the Twilio Video error codes.