# OTSession Class Reference

Inherits from | NSObject
Declared in | OTSession.h

## Overview

The first step in using the OpenTok iOS SDK is to initialize an OTSession object with your API key and a valid session ID Use the OTSession object to connect to OpenTok using your developer API key and a valid token.

## Getting information about the session

### `sessionConnectionStatus`

The status of this OTSession instance. Useful for ad-hoc queries about session status.

`@property (readonly) OTSessionConnectionStatus sessionConnectionStatus`

#### Discussion

Valid values are defined in OTSessionConnectionStatus:

- `OTSessionConnectionStatusNotConnected` - The session is not connected.
- `OTSessionConnectionStatusConnected` - The session is connected.
- `OTSessionConnectionStatusConnecting` - The session is connecting.
- `OTSessionConnectionStatusDisconnecting` - The session is disconnecting.
- `OTSessionConnectionStatusFailed` - The session has experienced a fatal error

On instantiation, expect the `sessionConnectionStatus` to have the value `OTSessionConnectionStatusNotConnected`.

You can use a key-value observer to monitor this property. However, the [OTSessionDelegate sessionDidConnect:] and [OTSessionDelegate sessionDidDisconnect:] messages are sent to the session’s delegate when the session connects and disconnects.

#### Declared In

`OTSession.h`

### `sessionId`

The session ID of this instance. This is an immutable value.

`@property (readonly) NSString *sessionId`

#### Declared In

`OTSession.h`

### `streams`

The streams that are a part of this session, keyed by streamId.

`@property (readonly) NSDictionary<NSString*OTStream*> *streams`

#### Declared In

`OTSession.h`

### `connection`

The OTConnection object for this session. The connection property is only available once the [OTSessionDelegate sessionDidConnect:] message is sent. If the session fails to connect, this property shall remain nil.

`@property (readonly) OTConnection *connection`

#### Declared In

`OTSession.h`

### `delegate`

The OTSessionDelegate object that serves as a delegate object for this OTSession object, handling messages on behalf of this session.

`@property (nonatomic, assign) id<OTSessionDelegate> _Nullable delegate`

#### Declared In

`OTSession.h`

### `apiQueue`

The delegate callback queue is application-definable. The GCD queue for issuing callbacks to the delegate may be overridden to allow integration with XCTest (new in XCode 5) or other frameworks that need the to operate in the main thread.

`@property (nonatomic, assign) dispatch_queue_t _Nonnull apiQueue`

#### Declared In

`OTSession.h`

### `capabilities`

An OTSessionCapabilities object, which indicates whether the client can publish and subscribe to streams in the session, based on the role assigned to the token used to connect to the session. This property is set to `nil` until you have connected to a session and the [OTSessionDelegate sessionDidConnect:] method has been called.

`@property (readonly) OTSessionCapabilities *capabilities`

#### Declared In

`OTSession.h`

## Initializing and connecting to a session

### `– initWithApiKey:sessionId:delegate:`

Initialize this session with your OpenTok API key , a session ID, and delegate before connecting to OpenTok. Send the [OTSession connectWithToken:error:] message to connect to the session.

`- (nullable id)initWithApiKey:(nonnull NSString *)apiKey sessionId:(nonnull NSString *)sessionId delegate:(nullable id<OTSessionDelegate>)delegate`

#### Parameters

`apiKey` | Your OpenTok API key. Important: If you are using a Vonage application (instead of an OpenTok project), pass in the application ID (not an OpenTok API key) for this parameter.
`sessionId` | The session ID of this instance.
`delegate` | The delegate (OTSessionDelegate) that handles messages on behalf of this session.

#### Return Value

The OTSession object, or nil if initialization fails.

#### Declared In

`OTSession.h`

### `– initWithApiKey:sessionId:delegate:settings:`

Initialize the OTSession object with settings defined by an OTSessionSettings object.

`- (nullable id)initWithApiKey:(nonnull NSString *)apiKey sessionId:(nonnull NSString *)sessionId delegate:(nullable id<OTSessionDelegate>)delegate settings:(nullable OTSessionSettings *)settings`

#### Parameters

`apiKey` | Your OpenTok API key. Important: If you are using the Video API with a Vonage application (instead of an OpenTok project), pass in the application ID (not an OpenTok API key) for this parameter.
`sessionId` | The session ID of this instance.
`delegate` | The delegate (OTSessionDelegate) object for the session.
`settings` | The (OTSessionSettings) object that defines settings for the session.

#### Declared In

`OTSession.h`

### `– connectWithToken:error:`

Once your application has a valid token, connect with your API key to begin participating in an OpenTok session.

`- (void)connectWithToken:(nonnull NSString *)token error:(OTError *_Nullable *_Nullable)error`

#### Parameters

`token` | The token generated for this connection.
`error` | Set if an error occurs synchronously while processing the request. The `OTSessionErrorCode` enum (defined in the OTError.h file) defines values for the `code` property of this object. This object is NULL if no synchronous error occurs. If an asynchronous error occurs, the [OTSessionDelegate session:didFailWithError:] message is sent to the session’s delegate.

#### Discussion

When the session connects successfully, the [OTSessionDelegate sessionDidConnect:] message is sent to the session’s delegate.

If the session cannot connect, the [OTSessionDelegate session:didFailWithError:] message is sent to the session’s delegate.

When the session disconnects, the [OTSessionDelegate sessionDidDisconnect:] message is sent to the session’s delegate.

Note that sessions automatically disconnect when the app is suspended.

Be sure to set up a delegate method for the [OTSessionDelegate session:didFailWithError:] message.

#### Declared In

`OTSession.h`

### `– disconnect:`

Disconnect from an active OpenTok session.

`- (void)disconnect:(OTError *_Nullable *_Nullable)error`

#### Parameters

`error` | Set if an error occurs synchronously while processing the request. The `OTSessionErrorCode` enum (defined in the OTError.h file) defines values for the `code` property of this object. This object is NULL if no error occurs.

#### Discussion

This method tears down all OTPublisher and OTSubscriber objects that have been initialized.

As a best practice, before calling this method, call the [OTSession unpublish:error:] method for all publishers and wait for the [OTPublisherKitDelegate publisher:streamDestroyed:] message. This ensures the complete removal of publishers, especially if network connectivity issues prevent removal due to the reconnection feature, which may keep publisher streams alive for potential reconnection.

When the session disconnects, the [OTSessionDelegate sessionDidDisconnect:] message is sent to the session’s delegate.

#### Declared In

`OTSession.h`

## Publishing audio-video streams to a session

### `– publish:error:`

Adds a publisher to the session.

`- (void)publish:(nonnull OTPublisherKit *)publisher error:(OTError *_Nullable *_Nullable)error`

#### Parameters

`publisher` | The OTPublisherKit object to stream with.
`error` | Set if an error occurs synchronously while processing the request. The `OTPublisherErrorCode` enum (defined in the OTError.h file) defines values for the `code` property of this object. This object is NULL if no error occurs. If an asynchronous error occurs, the [OTPublisherKitDelegate publisher:didFailWithError:] message is sent to the publisher’s delegate.

#### Discussion

When the publisher begins streaming data, the [OTPublisherKitDelegate publisher:streamCreated:] message is sent to the publisher delegate delegate.

If publishing fails, [OTPublisherKitDelegate publisher:didFailWithError:] is sent to the publisher delegate and no session delegate message will be passed.

Note that multiple publishers are not supported.

#### Declared In

`OTSession.h`

### `– unpublish:error:`

Removes a publisher from the session.

`- (void)unpublish:(nonnull OTPublisherKit *)publisher error:(OTError *_Nullable *_Nullable)error`

#### Parameters

`publisher` | The OTPublisher object to remove from the session.
`error` | Set if an error occurs synchronously while processing the request. The `OTPublisherErrorCode` enum (defined in the OTError.h file) defines values for the `code` property of this object. This object is NULL if no error occurs.

#### Discussion

Upon removing the publisher, the [OTPublisherKitDelegate publisher:streamDestroyed:] message is sent to the publisher delegate after streaming has stopped.

#### Declared In

`OTSession.h`

## Subscribing to audio-video streams

### `– subscribe:error:`

Connects this subscriber instance to the session and begins subscribing. If the subscriber passed is created from an `OTStream` instance from a different `OTSession` instance, the behavior of this function is undefined.

`- (void)subscribe:(nonnull OTSubscriberKit *)subscriber error:(OTError *_Nullable *_Nullable)error`

#### Parameters

`subscriber` | The subscriber to connect and begin subscribing.
`error` | Set if an error occurs synchronously while processing the request. The `OTSubscriberErrorCode` enum (defined in the OTError.h file) defines values for the `code` property of this object. This object is NULL if no error occurs. If an asynchronous error occurs, the [OTSubscriberKitDelegate subscriber:didFailWithError:] message is sent to the subscriber’s delegate.

#### Declared In

`OTSession.h`

### `– unsubscribe:error:`

Disconnects this subscriber instance from the session and begins object cleanup.

`- (void)unsubscribe:(nonnull OTSubscriberKit *)subscriber error:(OTError *_Nullable *_Nullable)error`

#### Parameters

`subscriber` | The subscriber to disconnect and remove from this session.
`error` | Set if an error occurs synchronously while processing the request. The `OTSubscriberErrorCode` enum (defined in the OTError.h file) defines values for the `code` property of this object. This object is NULL if no error occurs.

#### Declared In

`OTSession.h`

## Sending and receiving signals in a session

### `– signalWithType:string:connection:error:`

Sends a signal to one or more clients in a session.

`- (void)signalWithType:(NSString *_Nullable)type string:(NSString *_Nullable)string connection:(OTConnection *_Nullable)connection error:(OTError *_Nullable *_Nullable)error`

#### Parameters

`type` | The type of the signal. The type is also set in the [OTSessionDelegate session:receivedSignalType:fromConnection:withString:] message. The maximum length of the type string is 128 characters, and it must contain only letters (A-Z and a-z), numbers (0-9), “-”, “_”, and “~”.
`string` | The data to send. The limit to the size of data is 8KB.
`connection` | A destination OTConnection object. Set this parameter to nil to signal all participants in the session.
`error` | If sending a signal fails, this value is set to an OTError object. The OTSessionErrorCode enum (in OTError.h) includes OTSessionInvalidSignalType and OTSessionSignalDataTooLong constants for these errors. Note that success indicates that the options passed into the method are valid and the signal was sent. It does not indicate that the signal was sucessfully received by any of the intended recipients.

#### Discussion

See [OTSession signalWithType:string:connection:retryAfterReconnect:error:] and [OTSessionDelegate session:receivedSignalType:fromConnection:withString:].

#### Declared In

`OTSession.h`

### `– signalWithType:string:connection:retryAfterReconnect:error:`

Sends a signal to one or more clients in a session. This version of the method includes a `retryAfterReconnect` parameter.

`- (void)signalWithType:(NSString *_Nullable)type string:(NSString *_Nullable)string connection:(OTConnection *_Nullable)connection retryAfterReconnect:(BOOL)retryAfterReconnect error:(OTError *_Nullable *_Nullable)error`

#### Parameters

`type` | The type of the signal. The type is also set in the [OTSessionDelegate session:receivedSignalType:fromConnection:withString:] message. The maximum length of the type string is 128 characters, and it must contain only letters (A-Z and a-z), numbers (0-9), “-”, “_”, and “~”. See [OTSession signalWithType:string:connection:retryAfterReconnect:error:], [OTSessionDelegate session:receivedSignalType:fromConnection:withString:], and [OTSessionDelegate sessionDidBeginReconnecting:].
`string` | The data to send. The limit to the size of data is 8KB.
`connection` | A destination OTConnection object. Set this parameter to nil to signal all participants in the session.
`retryAfterReconnect` | Upon reconnecting to the session, whether to send any signals that were initiated while disconnected. If your client loses its connection to the OpenTok session, due to a drop in network connectivity, the client attempts to reconnect to the session, and the [OTSessionDelegate sessionDidBeginReconnecting:] message is sent. By default, signals initiated while disconnected are sent when (and if) the client reconnects to the OpenTok session. You can prevent this by setting the `retryAfterReconnect` parameter to `false`. (The default value is `true`.)
`error` | If sending a signal fails, this value is set to an OTError object. The OTSessionErrorCode enum (in OTError.h) includes OTSessionInvalidSignalType and OTSessionSignalDataTooLong constants for these errors. Note that success indicates that the options passed into the method are valid and the signal was sent. It does not indicate that the signal was sucessfully received by any of the intended recipients.

#### Declared In

`OTSession.h`

## Forcing streams to mute audio

### `– forceMuteAll:error:`

Forces all publishers in the session (except for those publishing excluded streams) to mute audio.

`- (void)forceMuteAll:(NSArray<OTStream*> *_Nullable)excludedStreams error:(OTError *_Nullable *_Nullable)error`

#### Parameters

`excludedStreams` | Streams to be exclued from the mute request. Set this to null to mute all streams in the session (including those published by the local client).
`error` | If the action fails, this value is set to an OTError object. Note that success indicates that the options passed into the method are valid and the request to mute streams was sent. It does not indicate that the request was successfully acted upon by the target clients.

#### Discussion

Also, any streams that are published after the call to the `[OTSession forceMuteAll:error:]` method are published with audio muted. You can remove the mute state of a session by calling the [OTSession disableForceMute:] method. After you call the [OTSession disableForceMute:] method, new streams published to the session will no longer have audio muted.

Calling this method causes the message to be sent in each client connected to the session, with the `active` property of the `muteForcedInfo` object set to `YES`.

Check the `canForceMute` property of the [OTSession capabilities] object to see if you can call this function successfully. This is reserved for clients that have connected with a token that has been assigned the moderator role (see the Token creation overview).

See [OTSession forceMuteStream:error:], [OTSession capabilities], [OTSessionDelegate session:muteForced:], [OTPublisherKitDelegate muteForced:], and Muting the audio of streams in a session.

#### Declared In

`OTSession.h`

### `– disableForceMute:`

Disables the active mute state of the session. After you call this method, new streams published to the session will no longer have audio muted.

`- (void)disableForceMute:(OTError *_Nullable *_Nullable)error`

#### Parameters

`error` | If the action fails, this is set to an OTError object.

#### Discussion

After you call the [OTSession forceMuteAll:error:] method (or a moderator in another client makes a call to mute all streams), any streams published after the moderation call are published with audio muted. Call the `disableForceMute()` method to remove the mute state of a session (so that new published streams are not automatically muted).

Calling this method causes the message to be sent in each client connected to the session, with the `active` property of the `muteForcedInfo` object set to `NO`.

Check the `canForceMute` property of the [OTSession capabilities] object to see if you can call this function successfully. This is reserved for clients that have connected with a token that has been assigned the moderator role (see the Token creation overview).

See [OTSession forceMuteAll:error:], [OTSession capabilities], [OTSessionDelegate session:muteForced:], and Muting the audio of streams in a session.

#### Declared In

`OTSession.h`

### `– forceMuteStream:error:`

Forces the publisher of a specified stream to mute its audio.

`- (void)forceMuteStream:(nonnull OTStream *)stream error:(OTError *_Nullable *_Nullable)error`

#### Parameters

`stream` | The stream to be muted.
`error` | If the action fails, this value is set to an OTError object. Note that success indicates that the options passed into the method are valid and the request to mute streams was sent. It does not indicate that the request was successfully acted upon by the target clients.

#### Discussion

Check the `canForceMute` property of the [OTSession capabilities] object to see if you can call this function successfully. This is reserved for clients that have connected with a token that has been assigned the moderator role (see the Token creation overview).

See [OTSession forceMuteAll:error:], [OTSession capabilities], [OTSessionDelegate session:muteForced:], [OTPublisherKitDelegate muteForced:], and Muting the audio of streams in a session.

#### Declared In

`OTSession.h`

## Forcing clients to disconnect

### `– forceDisconnect:error:`

Forces a client to disconnect from the session.

`- (void)forceDisconnect:(OTConnection *_Nullable)connection error:(OTError *_Nullable *_Nullable)error`

#### Parameters

`connection` | The OTConnection to disconnect.
`error` | If force disconnect fails, this value is set to an OTError object. The OTSessionErrorCode enum (in OTError.h) includes OTSessionUnableToForceDisconnect for these errors.

#### Discussion

While you can use the [OTSession forceDisconnect:error:] method to terminate your own connection, calling the [OTSession disconnect:] method is simpler. An error is returned if the client’s role does not include permissions required to force other users to disconnect. You define a client’s role when you create the user token. When a connection is terminated using this method , [OTSessionDelegate sessionDidDisconnect:], [OTSessionDelegate session:connectionDestroyed:] and [OTPublisherKitDelegate publisher:streamDestroyed:] messages are dispatched in the same way as they would be if the connection had terminated itself using [OTSession disconnect:]

#### Declared In

`OTSession.h`

## Reporting an issue

### `– reportIssue:`

Report that your app experienced an issue. You can use the issue ID with the Inspector or when discussing an issue with the Vonage Video API support team.

`- (void)reportIssue:(NSString *_Nullable *_Nullable)issueId`

#### Parameters

`issueId` | A pointer to a string that will be set the unique identifier for the reported issue. If the call to the method fails (for example, because of no network connection), this value is set to nil.

#### Declared In

`OTSession.h`

### `– setEncryptionSecret:error:`

Sets the end-to-end encryption secret used by all publishers and subscribers.

`- (void)setEncryptionSecret:(nonnull NSString *)secret error:(OTError *_Nullable *_Nullable)error`

#### Parameters

`secret` | Value of the encryption secret.
`error` | If the action fails, this parameter is set to an OTError object.

#### Discussion

See the End-to-end encryption developer guide.

#### Declared In

`OTSession.h`