Can I connect to livekit room using nodejs and als...
# ask-ai
w
Can I connect to livekit room using nodejs and also I would like to send and receive audio and other types of media from nodejs server which will be connecting to livekit root
t
Thanks for reaching out, Rohit. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Absolutely! You can connect to a LiveKit room using Node.js and send or receive audio and other media types. The
@livekit/rtc-node
SDK makes it easy for your Node.js app to connect to a LiveKit server, publish audio/video tracks, and subscribe to media from other participants. Here’s a quick rundown to get you started: • Connecting to a Room: Use the
Room
class to join a room. Here’s a simple example:
Copy code
import { Room } from '@livekit/rtc-node';

const room = new Room();
await room.connect(url, token, { autoSubscribe: true, dynacast: true });
• Sending Audio: Create an
AudioSource
and
LocalAudioTrack
, then publish it to the room. Like this:
Copy code
const source = new AudioSource(sampleRate, channels);
const track = LocalAudioTrack.createAudioTrack('audio', source);
await room.localParticipant.publishTrack(track, options);
await source.captureFrame(new AudioFrame(buffer, sampleRate, channels, sampleCount));
• Receiving Audio: You can subscribe to and handle incoming audio tracks easily. For example:
Copy code
room.on(RoomEvent.TrackSubscribed, (track) => {
  if (track.kind === TrackKind.KIND_AUDIO) {
    const stream = new AudioStream(track);
    for await (const frame of stream) {
      // Process audio frame
    }
  }
});
• Video Support: The SDK also supports video! You can manage video tracks similarly to audio, using the
RemoteVideoTrack
class to work with video from other participants. Just a heads up: the Node.js realtime SDK is currently in Developer Preview, so it’s best to avoid using it in production environments for now. Sources: examples/rpc/index.ts | Connecting to LiveKit | LiveKit Docs