Client & Authentication

Client & Auth

Before joining a call, it is necessary to set up the video client. Here’s a basic example:

let streamVideo = StreamVideo(
    apiKey: apiKey,
    user: user,
    token: token,
    tokenProvider: { _ in }
)
  • The API Key can be found in your dashboard.
  • The user can be either authenticated, anonymous or guest.
  • Note: You can store custom data on the user object, if required.

Typically, you’ll want to initialize the client in your app’s AppDelegate or SceneDelegate or in a dependency injection module.

Generating a token

Tokens need to be generated server side. You can use our server side SDKs to quickly add support for this. Typically you integrate this into the part of your codebase where you login or register users. The tokens provide a way to authenticate a user or give access to a specific set of calls.

Here’s a valid user and token to help you get started on the client side, before integrating with your backend API.

Different types of users

Authenticated users are users that have an account on your app. Guest users are temporary user accounts. You can use it to temporarily give someone a name and image when joining a call. Anonymous users are users that are not authenticated. It’s common to use this for watching a livestream or similar where you aren’t authenticated.

This example shows the client setup for a guest user:

let streamVideo = StreamVideo(
    apiKey: apiKey,
    user: .guest("guest"),
    token: token,
    tokenProvider: { _ in }
)

And here’s an example for an anonymous user:

let streamVideo = StreamVideo(
    apiKey: apiKey,
    user: .anonymous,
    token: token,
    tokenProvider: { _ in }
)

Anonymous users don’t establish a web socket connection, therefore they won’t receive any events. They are just able to watch a livestream or join a call.

The token for an anonymous user should contain the call_cids field, which is an array of the call cid’s that the user is allowed to join.

Call cid consists of the call type and the call id, in the following format: callType:callId.

Here’s an example JWT token payload for an anonymous user:

{
  "iss": "@stream-io/dashboard",
  "iat": 1726406693,
  "exp": 1726493093,
  "user_id": "!anon",
  "role": "viewer",
  "call_cids": [
    "livestream:123"
  ]
}

If you try to join a call that is not listed in the array, you will receive an error.

Client options

Here’s a more complete example of the client options:

let streamVideo = StreamVideo(
    apiKey: apiKey,
    user: user,
    token: token,
    videoConfig: VideoConfig(),
    pushNotificationsConfig: .default,
    tokenProvider: { _ in }
)

The full list of supported options is:

OptionDescriptionDefault
apiKeyThe API key to use. Found in the dashboard-
userThe user object. You can store custom data on the user.-
tokenThe JWT token to use for authentication.-
videoConfigA VideoConfig instance representing the current video config.VideoConfig()
pushNotificationsConfigConfig for push notifications..default
tokenProviderA function to call if the token is expired or invalid.-
© Getstream.io, Inc. All Rights Reserved.