\OpenTokOpenTok

Contains methods for creating OpenTok sessions, generating tokens, and working with archives.

To create a new OpenTok object, call the OpenTok() constructor with your OpenTok API key and the API secret for your TokBox account. Do not publicly share your API secret. You will use it with the OpenTok() constructor (only on your web server) to create OpenTok sessions.

Be sure to include the entire OpenTok server SDK on your web server.

Summary

Methods
Properties
Constants
generateToken()
createSession()
startArchive()
stopArchive()
getArchive()
deleteArchive()
listArchives()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
No private methods found
No private properties found
N/A

Methods

generateToken()

generateToken(string $sessionId, array $options) : string

Creates a token for connecting to an OpenTok session. In order to authenticate a user connecting to an OpenTok session, the client passes a token when connecting to the session.

For testing, you can also generate tokens by logging in to your TokBox account.

Parameters

string $sessionId

The session ID corresponding to the session to which the user will connect.

array $options

This array defines options for the token. This array includes the following keys, all of which are optional:

  • 'role' (string) — One of the constants defined in the RoleConstants class. The default role is publisher
  • 'expireTime' (int) — The timestamp for when the token expires, in milliseconds since the Unix epoch. The default expiration time is 24 hours after the token creation time. The maximum expiration time is 30 days after the token creation time.
  • 'data' (string) — A string containing connection metadata describing the end-user. For example, you can pass the user ID, name, or other data describing the end-user. The length of the string is limited to 1000 characters. This data cannot be updated once it is set.

Returns

string —

The token string.

createSession()

createSession(array $options) : \OpenTok\Session

Creates a new OpenTok session and returns the session ID, which uniquely identifies the session.

For example, when using the OpenTok JavaScript library, use the session ID when calling the OT.initSession() method (to initialize an OpenTok session).

OpenTok sessions do not expire. However, authentication tokens do expire (see the generateToken() method). Also note that sessions cannot explicitly be destroyed.

A session ID string can be up to 255 characters long.

Calling this method results in an OpenTokException in the event of an error. Check the error message for details.

You can also create a session using the OpenTok REST API or the by logging in to your TokBox account.

Parameters

array $options

(Optional) This array defines options for the session. The array includes the following keys (all of which are optional):

  • 'archiveMode' (ArchiveMode) — Whether the session is automatically archived (ArchiveMode::ALWAYS) or not (ArchiveMode::MANUAL). By default, the setting is ArchiveMode.MANUAL, and you must call the OpenTok->startArchive() method to start archiving. To archive the session (either automatically or not), you must set the mediaMode key to MediaMode::ROUTED.
  • 'location' (String) — An IP address that the OpenTok servers will use to situate the session in its global network. If you do not set a location hint, the OpenTok servers will be based on the first client connecting to the session.
  • 'mediaMode' (MediaMode) — Whether the session will transmit streams using the OpenTok Media Router (MediaMode.ROUTED) or not (MediaMode.RELAYED). By default, the mediaMode property is set to MediaMode.RELAYED.

    With the mediaMode parameter set to MediaMode.RELAYED, the session will attempt to transmit streams directly between clients. If clients cannot connect due to firewall restrictions, the session uses the OpenTok TURN server to relay audio-video streams.

    The OpenTok Media Router provides the following benefits:

    • The OpenTok Media Router can decrease bandwidth usage in multiparty sessions. (When the mediaMode parameter is set to MediaMode.ROUTED, each client must send a separate audio-video stream to each client subscribing to it.)
    • The OpenTok Media Router can improve the quality of the user experience through recovery. With these features, if a client's connectivity degrades to a degree that it does not support video for a stream it's subscribing to, the video is dropped on that client (without affecting other clients), and the client receives audio only. If the client's connectivity improves, the video returns.
    • The OpenTok Media Router supports the archiving feature, which lets you record, save, and retrieve OpenTok sessions.

Returns

\OpenTok\Session

A Session object representing the new session. Call the getSessionId() method of this object to get the session ID. For example, when using the OpenTok.js library, use this session ID when calling the OT.initSession() method.

startArchive()

startArchive(String $sessionId, array $options) : \OpenTok\Archive

Starts archiving an OpenTok session.

Clients must be actively connected to the OpenTok session for you to successfully start recording an archive.

You can only record one archive at a time for a given session. You can only record archives of sessions that use the OpenTok Media Router (sessions with the media mode set to routed); you cannot archive sessions with the media mode set to relayed.

For more information on archiving, see the OpenTok archiving programming guide.

Parameters

String $sessionId

The session ID of the OpenTok session to archive.

array $options

(Optional) This array defines options for the archive. The array includes the following keys (all of which are optional):

  • 'name' (String) — The name of the archive. You can use this name to identify the archive. It is a property of the Archive object, and it is a property of archive-related events in the OpenTok client SDKs.
  • 'hasVideo' (Boolean) — Whether the archive will record video (true, the default) or not (false). If you set both hasAudio and hasVideo to false, the call to the startArchive() method results in an error.
  • 'hasAudio' (Boolean) — Whether the archive will record audio (true, the default) or not (false). If you set both hasAudio and hasVideo to false, the call to the startArchive() method results in an error.
  • 'outputMode' (OutputMode) — Whether all streams in the archive are recorded to a single file (OutputMode::COMPOSED, the default) or to individual files (OutputMode::INDIVIDUAL).

    Returns

    \OpenTok\Archive

    The Archive object, which includes properties defining the archive, including the archive ID.

    stopArchive()

    stopArchive(String $archiveId) : \OpenTok\Archive

    Stops an OpenTok archive that is being recorded.

    Archives automatically stop recording after 120 minutes or when all clients have disconnected from the session being archived.

    Parameters

    String $archiveId

    The archive ID of the archive you want to stop recording.

    Returns

    \OpenTok\Archive

    The Archive object corresponding to the archive being stopped.

    getArchive()

    getArchive(String $archiveId) : \OpenTok\Archive

    Gets an Archive object for the given archive ID.

    Parameters

    String $archiveId

    The archive ID.

    Throws

    \OpenTok\ArchiveException

    There is no archive with the specified ID.

    \OpenTok\OpenTokArgumentException

    The archive ID provided is null or an empty string.

    Returns

    \OpenTok\Archive

    The Archive object.

    deleteArchive()

    deleteArchive(String $archiveId) : Boolean

    Deletes an OpenTok archive.

    You can only delete an archive which has a status of "available", "uploaded", or "deleted". Deleting an archive removes its record from the list of archives. For an "available" archive, it also removes the archive file, making it unavailable for download. For a "deleted" archive, the archive remains deleted.

    Parameters

    String $archiveId

    The archive ID of the archive you want to delete.

    Throws

    \OpenTok\ArchiveException

    There archive status is not "available", "updated", or "deleted".

    Returns

    Boolean —

    Returns true on success.

    listArchives()

    listArchives(integer $offset, integer $count) : \OpenTok\ArchiveList

    Returns an ArchiveList. The <code>items()</code> method of this object returns a list of archives that are completed and in-progress, for your API key.

    Parameters

    integer $offset

    Optional. The index offset of the first archive. 0 is offset of the most recently started archive. 1 is the offset of the archive that started prior to the most recent archive. If you do not specify an offset, 0 is used.

    integer $count

    Optional. The number of archives to be returned. The maximum number of archives returned is 1000.

    Returns

    \OpenTok\ArchiveList

    An ArchiveList object. Call the items() method of the ArchiveList object to return an array of Archive objects.