Creating Sessions
Overview
A Vonage Video session is where users connect to interact in real-time. Your application server generates the session so that the clients can join.
This how-to will go over:
- Creating a session
Before you start
As previously mentioned, you will need an application server running to create a session.
Create a Session
When creating a session, there are a few options you can specify depending on your needs. They are media mode, archiving preference and location hint. You can find more information on these options in the Create a Session guide.
// Create a session that will attempt to transmit streams directly between
// clients. If clients cannot connect, the session uses the Vonage TURN server:
try {
const session = await vonage.video.createSession();
// save the sessionId
db.save("session", session.sessionId, done);
} catch(error) {
console.error("Error creating session: ", error);
}
// The session will use the Vonage Media Router:
try {
const session = await vonage.video.createSession({ mediaMode: MediaMode.ROUTED });
// save the sessionId
db.save("session", session.sessionId, done);
} catch(error) {
console.error("Error creating session: ", error);
}
// A Session with a location hint
try {
const session = await vonage.video.createSession({ location: "12.34.56.78" });
// save the sessionId
db.save("session", session.sessionId, done);
} catch(error) {
console.error("Error creating session: ", error);
}
// A Session with an automatic archiving
try {
const session = await vonage.video.createSession({ mediaMode: MediaMode.ROUTED, archiveMode: "always" });
// save the sessionId
db.save("session", session.sessionId, done);
} catch(error) {
console.error("Error creating session: ", error);
}
// A session that attempts to stream media directly between clients:
CreateSessionResponse session = videoClient.createSession();
// A session that uses the Vonage Video Media Router:
CreateSessionResponse session = videoClient.createSession(
CreateSessionRequest.builder().mediaMode(MediaMode.ROUTED).build()
);
// A Session with a location hint:
CreateSessionResponse session = videoClient.createSession(
CreateSessionRequest.builder().location("12.34.56.78").build()
);
// A session that is automatically archived (it must used the routed media mode)
CreateSessionResponse session = videoClient.createSession(
CreateSessionRequest.builder()
.mediaMode(MediaMode.ROUTED)
.archiveMode(ArchiveMode.ALWAYS)
.build()
);
// Store this sessionId in the database for later use:
String sessionId = session.getSessionId();
// Create a session that will attempt to transmit streams directly between clients
Result<CreateSessionRequest> request = CreateSessionRequest.Default;
// Or create a custom request
Result<CreateSessionRequest> request = CreateSessionRequest.Build()
.WithLocation("192.168.1.1")
.WithMediaMode(MediaMode.Routed)
.WithArchiveMode(ArchiveMode.Always)
.Create();
// Send the request to the API
Result<CreateSessionResponse> response = await client.SessionClient.CreateSessionAsync(request);
use Vonage\Video\MediaMode;
use Vonage\Video\SessionOptions;
use Vonage\Video\Archive\ArchiveMode;
// Create a session that attempts to use peer-to-peer streaming:
$session = $client->video()->createSession();
// A session that uses the Vonage Video Media Router, which is required for archiving:
$session = $client->video()->createSession(new SessionOptions(['mediaMode' => MediaMode::ROUTED]));
// A session with a location hint:
$session = $client->video()->createSession(new SessionOptions(['location' => '12.34.56.78']));
// An automatically archived session:
$sessionOptions = new SessionOptions([
'archiveMode' => ArchiveMode::ALWAYS,
'mediaMode' => MediaMode::ROUTED
]);
$session = $client->video()->createSession($sessionOptions);
// Store this sessionId in the database for later use
$sessionId = $session->getSessionId();
from vonage_video.models import VideoSession
# Create a session
session_info: VideoSession = vonage_client.video.create_session()
# A session that doesn't use the Vonage Media Router and instead sends
# information between peers directly
from vonage_video.models import SessionOptions
options = SessionOptions(media_mode='relayed')
session_info = vonage_client.video.create_session(options)
# An automatically archived session:
options = SessionOptions(media_mode='routed', archive_mode='always')
session_info = vonage_client.video.create_session(options)
# A session with a location hint
options = SessionOptions(location='12.34.56.78')
session_info = vonage_client.video.create_session(options)
# Extract the session ID to be stored in a database
session_id = session_info.session_id
# Creating a session using the default settings (media_mode defaults to 'routed', which will use the Vonage Media Router, and archive_mode defaults to 'manual', which does not automatically create an archive):
session = client.video.create_session
# Creating a session that will attempt to transmit streams directly between clients (note that archive_mode MUST BE 'manual', which is the default, for media_mode to be 'relayed'):
session = client.video.create_session(media_mode: 'relayed')
# Creating a session with a location hint:
session = client.video.create_session(location: '12.34.56.78')
# Creating a session with automatic archiving (MUST use the 'routed' media mode, which is the default):
session = client.video.create_session(archive_mode: 'always')
# You can use the session_id accessor method of the returned Vonage::Response instance to get the session ID of the created session. This can be assigned to a variable or later use, such as when generating tokens:
session_id = session.session_id
In addition to the Server SDKs, a session can be created by making an API call to an endpoint. You can find more information in the Vonage Video API Reference
The session ID will be returned and can then be passed to the client to allow users to connect and interact with their tokens.