Android quick start

Getting started with Channels is very easy. However, if you have any questions get in touch. This guide uses the pusher-websocket-java library and a selection of Server API libraries. We also have a guide for our JavaScript client.

Get your free API keys

Create an account, and make a note of your app_id, app_key, app_secret and app_cluster.

Install the library

Gradle

In your build.gradle script for your application module, add the library as a dependency:

1
2
3
dependencies {
    compile 'com.pusher:pusher-java-client:1.6.0'
}

Maven

The pusher-websocket-java library is also available in Maven Central. Add a dependency element as a child of dependencies with the following in pom.xml:

1
2
3
4
5
<dependency>
  <groupId>com.pusher</groupId>
  <artifactId>pusher-java-client</artifactId>
  <version>1.6.0</version>
</dependency>

Create a connection

1
2
3
4
5
6
import com.pusher.client.Pusher;
PusherOptions options = new PusherOptions();
options.setCluster("APP_CLUSTER");

Pusher pusher = new Pusher("APP_KEY", options);
pusher.connect();

See more about configuring the java library

Subscribe to a public channel

1
2
3
4
import com.pusher.client.channel.Channel;
...

Channel channel = pusher.subscribe("my-channel");

Listen for events

Once you have created an instance of Channel, you can set up event bindings.

1
2
3
4
5
6
7
8
9
import com.pusher.client.channel.SubscriptionEventListener;
...

channel.bind("my-event", new SubscriptionEventListener() {
  @Override
  public void onEvent(String channelName, String eventName, final String data) {
    System.out.println(data);
  }
});

Trigger events from your server

In the examples below we trigger an event named my-event to Channels on a channel called my-channel. For each example below a server library deals with the server communication.

  • Rails
  • Ruby
  • PHP
  • Node.js
  • ASP.NET MVC
  • Python
  • Go
  • Java
  • Pusher CLI
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# First, run 'gem install pusher'

require 'pusher'

pusher_client = Pusher::Client.new(
  app_id: 'APP_ID',
  key: 'APP_KEY',
  secret: 'APP_SECRET',
  cluster: 'APP_CLUSTER'
)

class HelloWorldController < ApplicationController
  def hello_world
    pusher_client.trigger('my-channel', 'my-event', {:message => 'hello world'})
  end
end

If there isn’t an example in a language that you are familiar with then have a look on our server libraries page to see if anyone has created one in your language.

Where next?

Find out about all the cool stuff you can do withchannels. Learn how to exclude event recipients when publishing events.