Can we have a server using the server sdk, join a ...
# ask-ai
a
Can we have a server using the server sdk, join a room in passive mode? What’s the cost in that case? Is it possible for it to join multiple rooms?
t
Thanks for reaching out, Sebastián. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks for reaching out! Could you please help me understand what you mean by "passive mode" for a server joining a room? Are you thinking of a mode where the server doesn’t actively send or receive media streams, or is it something different? Also, I’d love to hear more about your use case for joining multiple rooms. Knowing a bit more will really help me guide you in the best way possible.
a
Yes, without sending or receiving audio, just seeing what’s going on in the room and send receive events or text. Multiple rooms means joining the room of different clients, asume each client connects its own room
t
Absolutely! A server using the LiveKit server SDK can definitely join a room without sending or receiving audio—just to observe and handle events or text data. You can do this by setting up the server participant's access token with permissions like these:
Copy code
CanPublish: false
CanSubscribe: false
CanPublishData: true
With these settings, your server can comfortably participate in text or byte streams for real-time data exchange, without dealing with any media. Also, the server SDK is quite flexible—it lets you connect to multiple rooms at the same time by creating separate room instances, each with their own unique identity and room name. This means a single server process can manage different rooms for different clients seamlessly. If you'd like, I’m here to help with more details or examples—just let me know! Sources: README.md | packages/livekit-server-sdk/README.md | livekit/tests/data_stream_test.rs | Sending text | LiveKit Docs | Sending files & bytes | LiveKit Docs
a
how would a server in NodeJS connect to a room?
t
Thanks for reaching out! Just to better assist you, could you please let me know if you're using a specific NodeJS library or SDK for LiveKit? If not, no worries at all—are you looking for some general guidance on how to connect to a room programmatically in NodeJS? I'm here to help!
a
The node server API sdk
t
I'd be happy to guide you through connecting to a room using the LiveKit Node.js server API SDK. Here’s a simple step-by-step approach: 1. First, import the necessary modules and create a
Room
object:
Copy code
import { Room } from '@livekit/rtc-node';

const room = new Room();
1. Next, use the
connect
method to establish your connection. You’ll need to provide the LiveKit server URL, an access token, and any optional settings you want to use:
Copy code
await room.connect(url, token, { autoSubscribe: true, dynacast: true });
Here,
url
is the WebSocket URL of your LiveKit server, and
token
is a JWT generated with the right permissions. 1. To generate that access token, you can use the
livekit-server-sdk
like this:
Copy code
import { AccessToken } from 'livekit-server-sdk';

const at = new AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET, {
  identity: 'user-identity',
});

at.addGrant({
  room: 'your-room-name',
  roomJoin: true,
  canPublish: false,
  canSubscribe: false,
  canPublishData: true,
});

const token = at.toJwt();
This token lets the server join the room and send or receive data messages, even if it’s not handling media itself. 1. Optionally, you can customize how the room behaves using the
opts
parameter, like turning on
autoSubscribe
or
dynacast
. 2. Don’t forget to add event listeners to manage room events such as
TrackSubscribed
or
Disconnected
. If you have any questions or want me to walk you through more examples, just let me know—I’m here to help! Sources: Connecting to LiveKit | LiveKit Docs | packages/livekit-server-sdk/README.md
a
and can the server join multiple rooms this way?
t
Absolutely! The server can definitely join multiple rooms using the Node.js server API SDK. What you’d do is create separate
Room
instances for each room you want to connect to. Each instance will independently connect to its own room with a unique identity and configuration, which gives you great flexibility. Here’s a simple example to illustrate:
Copy code
const room1 = new Room();
await room1.connect(url1, token1);

const room2 = new Room();
await room2.connect(url2, token2);
This way, your server can handle multiple rooms at the same time smoothly. If you have any questions or want me to walk you through more examples, just let me know—I’m here to help!
a
can we know from the server all the rooms currently open and if we're connected?
t
I'm happy to help you with this! The server using the Node.js server API SDK can list all currently open (active) rooms by using the
listRooms()
method from the
RoomServiceClient
class. When you call this method without any parameters, it will return all active rooms on the server. One thing to keep in mind is that the server SDK doesn’t provide a way to check if the server itself is connected to specific rooms. This is because the server SDK is designed to send commands to rooms through API calls, rather than maintaining real-time WebSocket connections like the client SDKs do. If you'd like, I’m here to provide more details or examples—just let me know! Sources: packages/livekit-server-sdk/src/RoomServiceClient.ts | test/singlenode_test.go | Managing rooms | LiveKit Docs | README.md | livekit/tests/room_test.rs
a
and how would you handling scaling a server too multiple instance but not having each instance connecting to the room twice?
t
Scaling a server to multiple instances without having each instance connect to the same room twice is totally doable, and LiveKit has some great options depending on your setup. Let me walk you through them: • Self-Hosted Setup: You’ll want to configure Redis for distributed mode. Think of Redis as a shared message bus that keeps everything in sync, making sure each room is hosted on just one node at a time. So, when a room is created, the system picks a single node to host it, and all participants connect there—keeping things neat and organized. • LiveKit Cloud: Here, LiveKit uses a distributed mesh architecture. This means participants can connect to different servers, but media is relayed smoothly between them. It’s a clever way to avoid duplication and keep performance optimized across regions. Both of these approaches help ensure your rooms don’t get duplicated across instances. If you’d like, I’m happy to dive deeper or help with specific implementation details—just let me know! Sources: How We Built a Globally Distributed WebRTC Mesh Network | Avoiding Room ID Reuse When Re-Creating Rooms With the Same Name | Getting Help & Troubleshooting | livekit/livekit_composite | DeepWiki | Distributed Setup | LiveKit Docs | delay-handling/raw_data.txt