We are excited to announce that we have published a major release of pusher-websocket-java, our library targeting Android and general Java.
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.
For Gradle, add the following to your app’s build.gradle
:
1repositories { 2 maven { url 'http://clojars.org/repo' } 3} 4 5dependencies { 6 compile 'com.pusher:pusher-java-client:1.0.0' 7}
For Maven, just create a dependency
node in your pom.xml
, between two dependencies
tags:
1<repositories> 2 <repository> 3 <id>clojars.org</id> 4 <url>http://clojars.org/repo</url> 5 </repository> 6</repositories> 7 8<dependencies> 9 <dependency> 10 <groupId>com.pusher</groupId> 11 <artifactId>pusher-java-client</artifactId> 12 <version>1.0.0</version> 13 </dependency> 14</dependencies>
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:
1import com.pusher.client.Pusher; 2 3Pusher pusher = new Pusher("YOUR_APP_KEY"); 4 5pusher.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
:
1import com.pusher.client.channel.Channel; 2import com.pusher.client.channel.SubscriptionEventListener; 3 4// subscribe to channel 5Channel channel = pusher.subscribe("my-channel"); 6 7// listen for an event 8channel.bind("my-event", new SubscriptionEventListener() { 9 10 @Override 11 public void onEvent(String channelName, String eventName, final String data) { 12 System.out.println(data); 13 } 14 15});
You can test this by sending messages with our Debug Console on your dashboard.
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.