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:
dependencies {
compile 'com.pusher:pusher-java-client:2.2.1'
}
∞ Maven
Add the following as a child of dependencies
in your pom.xml
:
<dependency>
<groupId>com.pusher</groupId>
<artifactId>pusher-java-client</artifactId>
<version>2.2.1</version>
</dependency>
∞ Create a connection
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
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.
import com.pusher.client.channel.SubscriptionEventListener;
...
channel.bind("my-event", new SubscriptionEventListener() {
@Override
public void onEvent(String channelName, String eventName, 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.
# First, run 'gem install pusher'
require 'pusher'
pusher = Pusher::Client.new(
app_id: 'APP_ID',
key: 'APP_KEY',
secret: 'APP_SECRET',
cluster: 'APP_CLUSTER'
)
pusher.trigger('my-channel', 'my-event', {:message => 'hello world'})
// First, run 'npm install pusher'
var Pusher = require("pusher");
var pusher = new Pusher({
appId: "APP_ID",
key: "APP_KEY",
secret: "APP_SECRET",
cluster: "APP_CLUSTER",
});
pusher.trigger("my-channel", "my-event", { message: "hello world" });
// First, run 'composer require pusher/pusher-php-server'
require __DIR__ . '/vendor/autoload.php';
$pusher = new Pusher\Pusher("APP_KEY", "APP_SECRET", "APP_ID", array('cluster' => 'APP_CLUSTER'));
$pusher->trigger('my-channel', 'my-event', array('message' => 'hello world'));
// First, run 'Install-Package PusherServer'
using PusherServer;
using System.Web.Mvc;
using System.Net;
using Your.Config;
public class HelloWorldController : Controller {
[httpPost]
public async Task<ActionResult> HelloWorld() {
var options = new PusherOptions();
options.Cluster = 'APP_CLUSTER';
var pusher = new Pusher('APP_ID', 'APP_KEY', 'APP_SECRET', options);
var result = await pusher.TriggerAsync("my-channel", "my-event", new { message = "hello world" });
return new HttpStatusCodeResult((int)HttpStatusCode.OK);
}
}
# First, run 'pip install pusher'
import pusher
pusher_client = pusher.Pusher(
app_id=u'APP_ID',
key=u'APP_KEY',
secret=u'APP_SECRET',
cluster=u'APP_CLUSTER'
)
pusher_client.trigger(u'my-channel', u'my-event', {u'message': u'hello world'})
// First, run 'go get github.com/pusher/pusher-http-go'
package main
import "github.com/pusher/pusher-http-go"
func main(){
pusherClient := pusher.Client{
AppId: "APP_ID",
Key: "APP_KEY",
Secret: "APP_SECRET",
Cluster: "APP_CLUSTER",
}
data := map[string]string{"message": "hello world"}
pusherClient.Trigger("my-channel", "my-event", data)
}
/*
First, add this Maven dependency:
<dependency>
<groupId>com.pusher</groupId>
<artifactId>pusher-http-java</artifactId>
<version>1.0.0</version>
</dependency>
*/
Pusher pusher = new Pusher("APP_ID", "APP_KEY", "APP_SECRET");
pusher.setCluster("APP_CLUSTER");
pusher.trigger("my-channel", "my-event", Collections.singletonMap("message", "Hello World"));
$ pusher channels apps trigger --app-id APP_ID --channel "my-channel" --event "my-event" --message "hello world"
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 with channels. Learn how to exclude event recipients when publishing events.