Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

Create Conference Calls in Java


In this guide we'll show you how to use Programmable Voice(link takes you to an external page) to create and manage conference calls in your Java web application. We'll also cover how to monitor your conference and its participants during the call. The code snippets in this guide are written using Java servlets and the Twilio SDK for Java(link takes you to an external page). Ready? Let's get started!

A simple conference call

a-simple-conference-call page anchor

An XML response for a simple conference call with TwiML


_10
<?xml version="1.0" encoding="UTF-8"?>
_10
<Response>
_10
<Dial>
_10
<Conference>My conference</Conference>
_10
</Dial>
_10
</Response>

Using TwiML Bin

using-twiml-bin page anchor

A handy tool we provide to host static TwiML from the Twilio Console is called TwiML Bin(link takes you to an external page).

Just go to the TwiML Bin page(link takes you to an external page) in the Developer Center and click the plus button to create a new TwiML Bin. You can then add any static TwiML you want to host. Here's an example:

Simple \<Conference> in a TwiML Bin.

Click "Save" and your TwiML Bin is ready to use with any of your Twilio phone numbers.

With our TwiML created and placed in a TwiMLBin, let's configure a phone number with it.

(warning)

Warning

If you are sending SMS messages to the U.S. or Canada, before proceeding further please be aware of updated restrictions on the use of Toll-Free numbers for messaging, including TF numbers obtained by purchasing them. These restrictions do not apply to Voice or other uses outside of SMS messaging. Please click here(link takes you to an external page) for details.


Buy and configure a phone number

buy-and-configure-a-phone-number page anchor

In the Twilio Console, you can search for and buy phone numbers in countries around the world. Numbers that have the Voice capability can make and receive voice phone calls from just about anywhere on the planet.

Search for voice capable numbers.

Once you purchase a number, you'll need to configure that number to send a request to your web application. This callback mechanism is called a webhook(link takes you to an external page). This can be done in the number's configuration page.

configure an incoming phone number URL.

Now give your number a call. You'll hear hold music when you first join — call in from another phone number and the conference call will begin.

TwiML Bins are great for setting up simple conference call lines, but with the power of Java we can do so much more. Let's see how.


Set up your web application

set-up-your-web-application page anchor
Incoming Voice.

Twilio makes answering a phone call as easy as responding to an HTTP request. When a phone number you have bought through Twilio receives an incoming call, Twilio will send an HTTP request to your web application asking for instructions on how to handle the call. Your server will respond with an XML document containing TwiML that instructs Twilio on what to do with the call. Those instructions can direct Twilio to read out a message, play an MP3 file, make a recording and much more.

To start answering phone calls, you must:

  • Buy and configure a Twilio-powered phone number(link takes you to an external page) capable of making and receiving phone calls, and point it at your web application
  • Write a web application to tell Twilio how to handle the incoming call using TwiML
  • Make your web application accessible on the Internet so Twilio can make an HTTP request when you receive a call

Dynamic conference calls with moderators

dynamic-conference-calls-with-moderators page anchor

Now comes the fun part - writing code that will handle an incoming HTTP request from Twilio! In this example we'll write a simple servlet to respond to Twilio's request, and we'll use the Twilio Java SDK(link takes you to an external page) to generate our TwiML.

Create a moderated conference call

create-a-moderated-conference-call page anchor

Use the <Conference> TwiML noun to create a conference that begins only when a moderator joins

Java

_56
import java.io.IOException;
_56
_56
import javax.servlet.ServletException;
_56
import javax.servlet.annotation.WebServlet;
_56
import javax.servlet.http.HttpServlet;
_56
import javax.servlet.http.HttpServletRequest;
_56
import javax.servlet.http.HttpServletResponse;
_56
_56
import com.twilio.twiml.voice.Conference;
_56
import com.twilio.twiml.voice.Dial;
_56
import com.twilio.twiml.TwiMLException;
_56
import com.twilio.twiml.VoiceResponse;
_56
_56
_56
@SuppressWarnings("serial")
_56
@WebServlet("/voice")
_56
public class IncomingCallServlet extends HttpServlet {
_56
_56
// Update with your own phone number in E.164 format
_56
public static final String MODERATOR = "+15558675310";
_56
_56
// Handle HTTP POST to /voice
_56
protected void doPost(HttpServletRequest request, HttpServletResponse response)
_56
throws ServletException, IOException {
_56
// Get the number of the incoming caller
_56
String fromNumber = request.getParameter("From");
_56
_56
Conference.Builder conferenceBuilder = new Conference.Builder("My Conference");
_56
_56
if (MODERATOR.equalsIgnoreCase(fromNumber)) {
_56
conferenceBuilder.startConferenceOnEnter(true);
_56
conferenceBuilder.endConferenceOnExit(true);
_56
} else {
_56
conferenceBuilder.endConferenceOnExit(false);
_56
}
_56
_56
// Create a TwiML builder object
_56
VoiceResponse twiml = new VoiceResponse.Builder()
_56
.dial(new Dial.Builder()
_56
.conference(conferenceBuilder.build())
_56
.build()
_56
).build();
_56
_56
// Render TwiML as XML
_56
response.setContentType("text/xml");
_56
_56
try {
_56
response.getWriter().print(twiml.toXml());
_56
} catch (TwiMLException e) {
_56
e.printStackTrace();
_56
}
_56
_56
_56
_56
}
_56
}

In this example we use a couple advanced <Conference> features to allow one participant, our "moderator," to better control the call:

  • startConferenceOnEnter will keep all other callers on hold until the moderator joins
  • endConferenceOnExit will cause Twilio to end the call for everyone as soon as the moderator leaves

We use the "From" argument on Twilio's webhook request to identify whether the current caller should be the moderator or just a regular participant.

In order for the webhooks in this code sample to work, Twilio must be able to send your web application an HTTP request over the Internet. Of course, that means your application needs to have a URL or IP address that Twilio can reach.

In production you probably have a public URL, but you probably don't during development. That's where ngrok(link takes you to an external page) comes in. ngrok gives you a public URL for a local port on your development machine, which you can use to configure your Twilio webhooks as described above.

Once ngrok is installed, you can use it at the command line to create a tunnel to whatever port your web application is running on. For example, this will create a public URL for a web application listening on port 3000.


_10
ngrok http 3000

After executing that command, you will see that ngrok has given your application a public URL that you can use in your webhook configuration in the Twilio console.

ngrok screen.

Grab your ngrok public URL and head back to the phone number you configured earlier. Now let's switch it from using a TwiML Bin to use your new ngrok URL. Don't forget to append the URL path to your actual TwiML logic! ("http://<your ngrok subdomain>.ngrok.io/voice" for example)

configure an incoming phone number URL.

You're now ready to host dynamic conference calls with your Java app. Grab some friends and give it a try!


If this guide was helpful, you might also want to check out these tutorials for Programmable Voice and Java. Tutorials walk through full sample applications, implementing Twilio use cases like these:

Happy hacking!


Rate this page: