We are excited to announce that we have published a major release of pusher-websocket-java, our library targeting Android and general Java. We thought our library, given its stability and use by many large customers in production, deserved a 1.0 release.

This blog post will provide a quick overview of how to use the Android library, and what you can do with it.

Installation

For Gradle, add the following to your app’s build.gradle:

repositories {
  maven { url 'http://clojars.org/repo' }
}

dependencies {
  compile 'com.pusher:pusher-java-client:1.0.0'
}

For Maven, just create a dependency node in your pom.xml, between two dependencies tags:

<repositories>
    <repository>
        <id>clojars.org</id>
        <url>http://clojars.org/repo</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
      <groupId>com.pusher</groupId>
      <artifactId>pusher-java-client</artifactId>
      <version>1.0.0</version>
    </dependency>
</dependencies>

Getting Started

Firstly, to initialize the client, create a new Pusher instance with your app key, which you can get by signing up for a free account, and calling connect() on the new object:

import com.pusher.client.Pusher;

Pusher pusher = new Pusher("YOUR_APP_KEY");

pusher.connect();

To start subscribing to a channel, call subscribe and pass in your channel name. Then we can start listening for events by binding to your specified event name and passing a new SubscriptionEventListener:

import com.pusher.client.channel.Channel;
import com.pusher.client.channel.SubscriptionEventListener;

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

// listen for an event
channel.bind("my-event", new SubscriptionEventListener() {

    @Override
    public void onEvent(String channelName, String eventName, final String data) {
        System.out.println(data);
    }

});

You can test this by sending messages with our Debug Console on your dashboard.

Over To You

You can find out much more via the Github repo. If you find anything lacking in the library, or room for improvement, please do send us a pull request!

We also released a tutorial on how to build an Android Chat app using the new library.

About Jamie Patel

Jamie is one of Pusher's Growth Engineers, and loves playing with new technology and working some realtime magic on them. Originally a literature graduate and founder of two magazines, he enjoys exploring the creative side of coding and is continually looking to learn new things.