I have a backend server, from which I need to conn...
# ask-ai
r
I have a backend server, from which I need to connect to livekit room. and pusblish audio data to the room how to do that ??
t
Thanks for reaching out, Mishal. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To connect a backend server to a LiveKit room and publish audio data, you can use the LiveKit server SDK. Below is a step-by-step guide using the Go SDK: 1. Install the SDK: Install the LiveKit server SDK for Go using:
go get <http://github.com/livekit/server-sdk-go/v2|github.com/livekit/server-sdk-go/v2>
go get <http://github.com/livekit/media-sdk/webm|github.com/livekit/media-sdk/webm>
2. Generate API Key and Secret: Use your LiveKit API credentials for authentication. 3. Connect to the Room: Use
lksdk.ConnectToRoom
with your server URL, API key, and secret. 4. Create an Audio Track: Use
lkmedia.NewPCMLocalTrack
to create an audio track specifying sample rate and channels. 5. Publish the Track: Publish the track to the room using
room.LocalParticipant.PublishTrack
. 6. Write Audio Samples: Continuously write audio samples to the track. 7. Handle Signals: Set up signal handling for graceful shutdown. Here is an example code snippet:
Copy code
package main

import (
    "<http://github.com/livekit/server-sdk-go/v2|github.com/livekit/server-sdk-go/v2>"
    "<http://github.com/livekit/server-sdk-go/v2/pkg/media|github.com/livekit/server-sdk-go/v2/pkg/media>"
)

func main() {
    room, _ := lksdk.ConnectToRoom("<ws://localhost:7880>", lksdk.ConnectInfo{
        APIKey:    "your_api_key",
        APISecret: "your_api_secret",
        RoomName:  "your_room_name",
        ParticipantIdentity: "backend-server",
    }, nil)

    track, _ := media.NewPCMLocalTrack(48000, 1, nil)
    room.LocalParticipant.PublishTrack(track, nil)

    // Write audio samples to the track here
}
Let me know if you need further assistance or clarification!
r
can you tell me who to do this in Javsacsript
t
Hey there! If you want to connect a backend server to a LiveKit room and publish audio data using JavaScript, here’s a simple step-by-step guide to help you out: 1. Install Dependencies: First, grab the necessary Node.js packages by running:
npm install @livekit/rtc-node livekit-server-sdk fs path
2. Generate a JWT Token: Use the
livekit-server-sdk
to create a token that lets you join and publish audio in the room. 3. Connect to the Room: Use the
Room
class from
@livekit/rtc-node
along with your token to connect to the LiveKit server. 4. Create and Publish an Audio Track: Use
AudioSource
and
LocalAudioTrack
to make an audio track and publish it to your room. 5. Send Audio Frames: Capture audio frames (like from an audio file) and send them to the track. Here’s a full example to get you started:
Copy code
import { Room, LocalAudioTrack, AudioSource, TrackPublishOptions } from '@livekit/rtc-node';
import { AccessToken } from 'livekit-server-sdk';
import fs from 'fs';

const URL = '<ws://your-livekit-server.com>';
const API_KEY = 'your-api-key';
const API_SECRET = 'your-api-secret';
const ROOM_NAME = 'your-room-name';

async function main() {
  const room = new Room();
  const token = new AccessToken(API_KEY, API_SECRET, { identity: 'backend-participant' });
  token.addGrant({ room: ROOM_NAME, roomJoin: true, canPublish: true });
  const jwt = await token.toJwt();

  await room.connect(URL, jwt);
  console.log('Connected to the room');

  const source = new AudioSource(16000, 1);
  const track = LocalAudioTrack.createAudioTrack('audio', source);
  const options = new TrackPublishOptions();
  await room.localParticipant.publishTrack(track, options);
  console.log('Audio track published successfully');

  const audioData = fs.readFileSync('path/to/audio.wav');
  const buffer = new Int16Array(audioData.buffer);
  for (let i = 0; i < buffer.length; i += 16000) {
    const frame = buffer.subarray(i, i + 16000);
    await source.captureFrame(frame);
  }

  await room.disconnect();
}

main().catch(console.error);
If anything’s unclear or you want a hand with any part of this, just let me know—I’m here to help!
r
How to view the live livekit logs ??
t
If you want to check out live logs in LiveKit, the Realtime Room Monitor app is a great tool—it gives you a handy console to watch real-time events as they happen: 1. Get to the Realtime Room Monitor: You can use the hosted version right here or run it on your own machine. 2. Set Up Your Credentials: Just click the gear icon up in the top-right corner, enter your LiveKit credentials, and save them. 3. Start Watching a Room: Pop in the
Room Name
and
Observer ID
, then hit "Start Observing" to connect. You’ll see live logs showing events, participant changes, and more. Or, if you prefer, you can use the LiveKit CLI to tail logs straight from an agent by running:
Copy code
lk agent logs
Feel free to reach out if you want a hand with anything else! Sources: agent-console/app/page.tsx
r
I have successfully able to connect to livekit and publish my audio, now I also have agent there how can I get response which is given inside the room to retrieved ???
t
Hey there! If you want to grab responses or messages from a LiveKit room in real-time, here are some easy ways to do it: 1. Using the
DataReceived
Event:
Just listen for the
DataReceived
event on the
Room
object. This event fires whenever someone else in the room sends data. `const room = new Room(); room.on(RoomEvent.DataReceived, (payload, participant, kind) => { const message = new TextDecoder().decode(payload); console.log(
Received message from ${participant.identity}:
, message); });` 2. Delivery Options: LiveKit offers both reliable and lossy delivery modes. For chat messages, reliable delivery is your best friend since it ensures messages arrive in order and don’t get lost. 3. Message Size Limits: To keep things smooth, try to keep messages under 15KiB for reliable delivery. If you’re using lossy delivery, stay under 1300 bytes to avoid any hiccups with fragmentation. If anything’s unclear or you want a hand with this, just let me know—I’m here to help!
r
like there are not text message but there audio published by the agent in the room
t
Here’s how you can grab and work with audio from participants in a LiveKit room: 1. Subscribe to Audio Tracks: Keep an eye on the
track_subscribed
event to know when someone shares an audio track. For example: `room.on(RoomEvent.TrackSubscribed, (track, publication, participant) => { if (track.kind === 'audio') { console.log(
Subscribed to audio track from ${participant.identity}
); // Process the audio track here } });` 2. Process Audio Frames: After subscribing, you can dive into the audio frames—maybe calculate dB levels, save the audio, or even play it live. 3. Handle Audio Stream Completion: Don’t forget to tidy up once the audio stream wraps up. If you want me to walk you through any part or need some tailored examples, just let me know—I’m here to help!
r
what is publication here ??
t
In LiveKit, a TrackPublication is basically a track that’s been shared in a room. It holds important info about the track and helps you manage its state and subscription easily. Here’s what it does: 1. Metadata Container: Keeps details like the track's ID (
sid
), name, kind (audio or video), source, and other useful info. 2. Subscription Management: Lets you subscribe or unsubscribe from the track and shows you if you’re currently subscribed. 3. State Control: Handles states like whether the track is muted, simulcasted, or encrypted. 4. Event Handling: Sends out events like
muted
,
unmuted
, and subscription changes, so your app can stay up-to-date in real time. 5. Track Association: Once you’re subscribed, it links to the actual media track, so you can play or work with the media directly. If you’d like me to walk you through it more or show some examples, just let me know—I’m happy to help!