-
See also:
Creates a new Jingle session jingle with the following configuration options:
jid
- The JID for the entity running the session jingle. May be either a{String}
or{JID}
.selfID
- An optional alternative tojid
, which MUST be a{String}
. By default, this is derived fromjid
, ifjid
was provided.iceServers
- An array of known ICE servers. SeeaddICEServer()
for the required format of each item.prepareSession
- See below for howprepareSession
worksperformTieBreak
- See below for howperformTieBreak
works
var Jingle = require('jingle');
var BaseSession = require('jingle-session');
var jingle = new Jingle({
jid: '[email protected]',
iceServers: [
{url: 'stun:stun.mydomain.example'}
],
prepareSession: function (opts) {
return new Session(opts);
}
});
opts
- An object summarizing the information in the session initiate request, suitable for passing directly to a session class constructor:sid
- The ID for the session, as provided by the initiating peer.peer
- The JID for the initiating peer (may be either a{String}
or{JID}
).peerID
- An alternative topeer
, which MUST be a{String}
(derived frompeer
).initiator
- This will always befalse
, as we are the one receiving the initiation request.applicationTypes
- An array of content application names.transportTypes
- An array of content transport names.
req
- The original session initiation request, in case you need more information than provided inopts
for selecting a session type.
Returns a Session
instance of your choosing or a falsy
value if you wish to fallback to a BaseSession
instance (the jingle-session
module).
The prepareSession()
function allows you to control what type of session is created for a given
incoming session request. If prepareSession()
is not included when creating the session jingle, or
if your prepareSession()
function does not return a session, a BaseSession
will be created for you.
new Jingle.Jingle({
...,
prepareSession: function (opts) {
// Check if the session request includes "stub" content, and if so
// create a custom StubSession.
if (opts.applicationTypes.indexOf('stub') >= 0) {
return new StubSession(opts);
}
}
});
existingSession
- A Session object.req
- The incoming session initiation request that triggered the tie break check.
A tie break check is performed when receiving a session-initiate
if:
- A session in the
pending
state already exists for the peer. - The existing pending session ID is greater than the one used by the incoming session request
The performTieBreak()
method allows you to control whether or not the session request is declined to resolve the tie. In some applications, you may wish to allow two simultaneous sessions (e.g., multiple uni-directional video sessions). In others you may wish to allow the tie break to force the use of a single session (e.g., a bidirectional video session).
Returning true
will trigger the tie break and deny the request; returning false
will allow the session request to proceed.
info
- Either a{String}
of the URI of the ICE server, or an object:url
- The URI of the ICE serverusername
- The username for accessing the ICE servercredential
- The password or other shared secret to authenticate theusername
with the ICE server
Saves the URI and any required credentials for an ICE STUN/TURN server for use by future sessions.
jingle.addICEServer('stun:stun.l.google.com:19302');
jingle.addICEServer({
url: 'stun:stun.mydomain.example',
username: 'ad24lwra',
credential: '234lamvnerl13k40au35oahfadad'
});
session
- A{BaseSession}
(or ideally, a subclass ofBaseSession
) instance
Calling this method allows the Jingle instance to begin tracking the session and route actions to it.
For incoming sessions, this step is already handled for you. Using this method is only necessary for when you are initiating a session yourself.
Relevant events generated by session
will now be proxied through the Jingle instance.
var session = new MyCustomJingleSession({
sid: 'sid123',
peer: '[email protected]',
initiator: true
});
jingle.addSession(session);
reason
- Why the sessions are being ended (seesession.end()
for the list of available reasons).silent
- Iftrue
, then session terminating messages will not be generated to be sent to the peers. This is only intended for cases where you've lost network connection and would not be able to send those messages anyway.
// End all sessions because we're about to go offline
jingle.endAllSessions('gone');
peer
- Either the{JID}
or{String}
value of the peer's ID.reason
- Why the session is being ended (seesession.end()
for the list of available reasons).reason
- Why the session is being ended. This may be either a{String}
or an object:condition
- The name of the reasontext
- A freeform description of the reasonalternativeSession
- If the condition isalternative-session
, this is thesid
value for that session.
silent
- Iftrue
, the session terminate message will not be generated to be sent to the peer.
// End sessions with a peer because we've successfully finished them.
jingle.endPeerSessions('[email protected]', 'success');
packet
- An object representing an action to process & apply to a session.from
- The ID for the peer which sent the packetto
- Optional, this should be the ID for the session jingleid
- ID of the packettype
- One of:set
,result
, orerror
jingle
- This is the data that will be forwarded to the matching sessionsid
- The session IDaction
- The action for the session to perform, using the data injingle
error
- Optional error object with a condition:condition
- The type of errorjingleCondition
- A Jingle specific error condition, if applicable
The .process()
method is the heart of how the Jingle module works. It verifies that the incoming packet is valid (e.g. preventing session hijacking or handling tie-breaking conditions), and then routes the packet to the matching session for further, session-type specific processing.
jingle.process({
to: '[email protected]',
from: '[email protected]',
id: '123',
type: 'set',
jingle: {
sid: 'sidABC',
action: 'session-initiate',
contents: [
{
application: {applicationType: 'stub'},
transport: {transportType: 'stub'}
}
]
}
});
The incoming
event is triggered when a session initiation request is received from a peer.
jingle.on('incoming', function (session) {
// Auto-accept sessions
session.accept();
});
The outgoing
event is triggered when a tracked session has its .start()
method called.
jingle.on('outgoing', function (session) {
renderOutgoingSessionUI(session);
});
The send
event provides a Jingle packet suitable for sending to the peer. Each packet includes some routing information, an optional Jingle payload, and an optional error payload.
jingle.on('send', function (data) {
realtimeConnection.send(data, function (err, result) {
// We want to process both successful acks, and errors
var resp = err || result;
// Ensure that we have the sid included in the ack so
// that it routes properly to the correct session.
resp.jingle = resp.jingle || {};
resp.jingle.sid = data.jingle.sid;
// Process the ack response
jingle.process(resp);
});
});
The terminated
event is triggered when a session has been ended, either locally via the session's .end()
method, or from a session terminate action from the peer.
jingle.on('terminated', function (session) {
closeSessionUI();
});
Log messages can be listened for with the log:error
, log:debug
, and log:info
events.
jingle.on('log:*', function (logLevel, msg) {
console.log(logLevel, msg);
});