Skip to main content
TRTC example includes server-side create/close, control WebSocket, and browser playback. Use Node.js 22. The API key stays on the server; the page receives only Session credentials. This local server binds to localhost and manages one session at a time.

1. Prepare the files

Save these three files in one directory. To start a new session directly, also save session.json from Quickstart.

server.mjs

index.html

main.js

2. Build and start

If you already created a session through the API, save the complete response as session-response.json and run this command. Start joins that saved session without creating another:
To let the player create a new session from session.json instead, run:
Choose one mode. Open http://localhost:3000 and click Start. Video plus the “Video playing” message confirms playback. Stop calls the server close endpoint and disconnects locally. A closed imported session cannot be started again; create one and save its new response.

3. FAQ

Creation succeeded. Why is there no video? Check that the session is still running and its returned transport matches this player. TRTC and Agora credentials are not interchangeable. Joining the room is not a first-frame signal; the example shows Video playing only after remote video starts rendering. Check SDK errors and the publisher_user_id subscription filter. Why is video visible but audio silent? The browser may block autoplay. Click Enable sound when it appears; you do not need a new session. This example plays remote media without opening the microphone. What should I do when Stop fails? Retry Stop if the request fails. A closing response means the closure request was accepted; retrieve the Session if you need to confirm its final state. Continue with conversation or external audio. See session management for server-side cleanup policies.

Closing the page

Users can close the page directly; the browser disconnects media and control. To end the server session automatically afterward, add "auto_close": {"disconnected_timeout_seconds": 60} at the top level of session.json. Closure starts after all control connections have been disconnected for 60 seconds. Use Stop when you want to end immediately. See session management for duration rules.