Get Started

The twilio.js Library: Twilio.Connection

A Twilio.Connection object represents a specific connection into Twilio, such as a call. You never instantiate it directly, but it's often passed in event handlers and one is returned when you call Twilio.Device.connect().

// Explicitly create a new connection
var connection = Twilio.Device.connect();

Twilio.Device.incoming(function(conn) {
  // conn is a Twilio.Connection object
});

Method reference

The following is a list of methods available on a Twilio.Connection object. The .accept() and .disconnect() methods behave similarly to jQuery's click method. When passed a function, these methods register that function as a handler to be called when an event occurs. Calling the method without passing a function triggers that event handler.

.accept( handler(connection) )

Register a handler function to be called when this connection is accepted. Accepting a connection sets its status to connecting.

The handler function receives the Twilio.Connection object as its sole argument.

.accept()

Accept a pending connection.

.reject()

Reject a pending connection. This will cause a hangup to be issued from the client session to the dialing party. If multiple client sessions are active the pending connection will be rejected for all of them.

.disconnect( handler(connection) )

Register a handler function to be called when this connection is closed.

The handler function receives the Twilio.Connection object as its sole argument.

var connection = Twilio.Device.connect();

connection.disconnect(function(conn) {
  console.log("the call has ended");
});

.disconnect()

Close this connection.

.error( handler(error) )

Register a handler function to be called when any asynchronous device error occurs during the lifetime of this connection. These may be errors in your request, your capability token, connection errors, or other application errors. See the Twilio Client error code reference for more information. Using the error handler is a great way to debug your application and help catch mistakes in your code!

The handler function receives an error object as an argument. The error object may include the following properties:

Property Description
message A string describing the error.
code A numeric error code described in the Twilio Client error code reference.
info A copy of the Flash info object produced by the events around the flash.net.NetConnection object.
connection A reference to the Twilio.Connection object that was active when the error occured.

.mute()

Stop capturing audio from the microphone for this connection.

.unmute()

Resume capturing audio from the microphone for this connection.

.sendDigits( digits )

Play DTMF tones. The digits parameter is a string and can contain the characters 0-9, #, and *. If you're familiar with TwiML, you can think of the sendDigits method as the sendDigits attribute in the <Number> Noun.

.status()

Return the status of this connection. The status will be one of the following strings: "pending", "connecting", "open" or "closed".

"pending"

The connection is incoming and hasn't yet been established.

"connecting"

The connection is transitioning to open status.

"open"

The connection has been established.

"closed"

The connection has been disconnected.

Property Reference

.parameters

An object available on incoming connections that contains application parameters such as who is calling and what was dialed. The parameters object includes the following properties:

Parameter Description
CallSid A unique identifier for this call, generated by Twilio.
AccountSid Your Twilio account id. It is 34 characters long, and always starts with the letters AC.
From The phone number or client identifier of the party that initiated the call. Phone numbers are formatted with a '+' and country code, e.g. +16175551212 ([E.164][e164] format). Client identifiers begin with the client: URI scheme; for example, for a call from a client named 'tommy', the From parameter will be client:tommy.
To The client identifier of the called party. Client identifiers begin with the client: URI scheme; for example, for a call to a client named 'jenny', the To parameter will be client:jenny.
ApiVersion The version of the Twilio API used to handle this call.