You can add group chat to your website using Twilio Client and conference rooms. Twilio conferences work with browsers and traditional phone calls so you can also provide a call in number for non web users to join in the fun. In this example all website users are placed in the same conference room, but you can also choose to place users separately into selectively named conference rooms.
A user loads a web page and is immediately joined into a conference call. If the user is the first person to join they will hear hold music. The conference begins when a second participant arrives. Status text shows the current state of the call.
This HowTo demonstrates browser calling using Twilio Client, specifying an outgoing connection to a Twilio Applications, and creating conference calls using TwiML <Conference>.
Create a capability token to securely bind Twilio Client to your account and also give the device the ability to make outbound calls to a Twilio Application.
include 'Services/Twilio/Capability.php';
$accountSid = 'ACxxxxxxxxxxxxxxx';
$authToken = 'xxxxxxxxxxxxxxxxx';
$token = new Services_Twilio_Capability($accountSid, $authToken);
$token->allowClientOutgoing('APxxxxxxxxxxxxxxx');
<script type="text/javascript" src="//static.twilio.com/libs/twiliojs/1.1/twilio.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
Twilio.Device.setup("<?php echo $token->generateToken();?>");
Twilio.Device.ready(function (device) {
$('#status').text('Ready to join conference');
Twilio.Device.connect();
});
Twilio.Device.offline(function (device) {
$('#status').text('Connection offline');
});
Twilio.Device.error(function (error) {
$('#status').text(error);
});
Twilio.Device.connect(function (conn) {
$('#status').text("Successfully joined conference");
});
Twilio.Device.disconnect(function (conn) {
$('#status').text("Disconnected");
});
});
</script>
<div id="status">
Connection offline
</div>
Twilio requests the Voice Url specified in the Application for instructions on how to handle the call logic.
The Voice URL specified in the Application will respond to Twilio's request with TwiML.
<Response>
<Dial>
<Conference>Client</Conference>
</Dial>
</Response>
Our TwiML instructs Twilio to put the caller in a <Conference> room named 'Client'. For a complete overview of TwiML (Twilio's XML) see the TwiML documentation.