---
"@context": https://schema.org
"@type": TechArticle
"@id": https://www.twilio.com/docs/video/node-best-practices#article
headline: Node.js Media SDK best practices
description: Recommendations for building reliable server-side media apps with the Video Media SDK for Node.js.
url: https://www.twilio.com/docs/video/node-best-practices
inLanguage: en
dateModified: 2026-09-15T15:39:36.000Z
author:
  "@type": Organization
  name: Twilio Developer Education Team
publisher:
  "@type": Organization
  name: Twilio
---

# 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

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`][] and its [`helpers/paced-audio-writer.js`][] in the [SDK repository][].

## Manage memory around frame buffers

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.

## Manage connection state

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.

## Clean up resources

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.

## Log and handle errors

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.

[SDK repository]: https://github.com/twilio/twilio-video-node/tree/main/examples

[`helpers/paced-audio-writer.js`]: https://github.com/twilio/twilio-video-node/blob/main/examples/helpers/paced-audio-writer.js

[`audio_push.js`]: https://github.com/twilio/twilio-video-node/blob/main/examples/audio_push.js
