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.
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.
A busy Room produces many large video frames.
- Avoid retaining any frame
Bufferpast theonFrame()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
reconnectingandreconnectedevents. - To release resources, listen for the
disconnectedevents.
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
TwilioErrorpassed to thedisconnectedandconnectFailureevents, and branch on its subclass, such asAccessTokenInvalidError,RoomNotFoundError, orSignalingConnectionError. - The
ErrorCodeenum lists the Twilio Video error codes.