Skip to content
Docs
Guides

Build with Go

Issue participant grants from a Go backend and join a dTelecom room from a Go process with ranked fallback URLs.

The Go module has two entry points. Import github.com/dtelecom/server-sdk-go/cloud to issue tokens, manage participants, verify webhooks or call Cloud AI services. Import the root package when your process joins a room to send or receive media and data. Both are distributed in the same Go module.

From your Go project, install the released version:

go get github.com/dtelecom/server-sdk-go@v1.3.1

Set DTELECOM_CLOUD_URL=https://cloud.dtelecom.org, DTELECOM_API_KEY and DTELECOM_API_SECRET on the backend. Create a client with cloud.New(cloud.Options{}) and reuse it across requests.

Call this function from your authenticated backend after authorizing the room and deriving the participant identity. Pass the client’s public IP when available so Cloud can choose a nearby primary node. An empty IP selects a node near the caller.

package grants
import (
"context"
"time"
"github.com/dtelecom/server-sdk-go/cloud"
)
type Grant struct {
Token string `json:"token"`
WsURL string `json:"wsUrl"`
FallbackURLs []string `json:"fallbackUrls"`
}
func Issue(ctx context.Context, client *cloud.Client, room, identity, clientIP string) (Grant, error) {
t, err := client.CreateToken(ctx, cloud.TokenOptions{
Room: room, Identity: identity, ClientIP: clientIP, TTL: 10 * time.Minute,
})
if err != nil {
return Grant{}, err
}
return Grant{Token: t.Token, WsURL: t.WsURL, FallbackURLs: t.FallbackURLs}, nil
}

Encode the returned struct as JSON and give it to that participant. The ordered list starts with the primary URL. Pass all three fields to the browser or mobile client. For calls that can outlast the grant, keep the session ID and expiry returned by CreateToken on the backend and extend the session before expiry.

To connect a Go process, save this as participant/main.go in your module and run go run ./participant. Join a second participant to the same project and room with its own identity to exchange media or data. Press Ctrl+C to leave.

package main
import (
"context"
"log"
"os"
"os/signal"
lksdk "github.com/dtelecom/server-sdk-go"
"github.com/dtelecom/server-sdk-go/cloud"
)
func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
client, err := cloud.New(cloud.Options{})
if err != nil {
log.Fatal(err)
}
grant, err := client.CreateToken(ctx, cloud.TokenOptions{Room: "demo", Identity: "go-worker"})
if err != nil {
log.Fatal(err)
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
callbacks := lksdk.NewRoomCallback()
callbacks.OnDisconnected = cancel
room, err := lksdk.ConnectToRoomWithToken(grant.FallbackURLs, grant.Token, callbacks)
if err != nil {
log.Fatal(err)
}
defer room.Disconnect()
log.Print("Joined demo as go-worker")
<-ctx.Done()
}

For a media publisher, see the tagged track examples. From a backend, use client.RoomService() to look up participants or manage an active room. The runnable Go administration example lists the participants in a room by name.