iOS quick start

Getting started with Pusher is very easy. However, if you have any questions get in touch.

This guide uses the PusherSwift client API and a selection of Server API libraries. We also have a guide for our JavaScript and Android.

Get your free API keys

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

Install the library

Install using CocoaPods.

pod 'PusherSwift', '3.2.0'

Import Pusher into the class that wants to make use of the library.

import PusherSwift
#import "PusherSwift/PusherSwift-Swift.h"

Here is a step-by-step guide on how to install and setup CocoaPods if you are using it for the first time.

Create a connection

let pusher = Pusher(key: "YOUR_APP_KEY")
pusher.connect()
Pusher *pusher = [[Pusher alloc] initWithKey:@"YOUR_APP_KEY"];
[pusher connect];

If your app is created in a different cluster to the default us-east-1, you must specify the cluster param like so:

let options = PusherClientOptions(
    host: .cluster("eu")
)

let pusher = Pusher(key: "YOUR_APP_KEY", options: options)
pusher.connect()
OCAuthMethod *authMethod = [[OCAuthMethod alloc] initWithAuthEndpoint:@"https://your.authendpoint/pusher/auth"];
OCPusherHost *host = [[OCPusherHost alloc] initWithCluster:@"eu"];
PusherClientOptions *options = [[PusherClientOptions alloc]
                                initWithOcAuthMethod:authMethod
                                attemptToReturnJSONObject:YES
                                autoReconnect:YES
                                ocHost:host
                                port:nil
                                encrypted:YES];

Pusher *pusher = [[Pusher alloc] initWithAppKey:@"YOUR_APP_KEY" options:options];
[pusher connect];

For detailed information see the connection docs and the PusherSwift documentation.

Subscribe to a public channel

let myChannel = pusher.subscribe("my-channel")
PusherChannel *myChannel = [pusher subscribeWithChannelName:@"my-channel"];

Listen for events

Once you have created a channel instance, you can set up event bindings. There is no need to wait for the connection to be established. The following example just recieves and prints a hello world message.

let _ = myChannel.bind(eventName: "my-event", callback: { (data: Any?) -> Void in
    if let data = data as? [String : AnyObject] {
        if let message = data["message"] as? String {
            print(message)
        }
    }
})
[myChannel bindWithEventName:@"my-event" callback:^void (NSDictionary *data) {
    NSString *message = data[@"message"];
    NSLog(message);
}];

Trigger events from your server

In the examples below we trigger an event named my_event to Pusher on a channel called test_channel. For each example below a server library deals with the server communication.

require 'pusher'

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

class HelloWorldController < ApplicationController
  def hello_world
    pusher_client.trigger('test_channel', 'my_event', {:message => 'hello world'})
  end
end
require 'pusher'

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

Pusher.trigger('test_channel', 'my_event', {:message => 'hello world'})
require('Pusher.php');

$pusher = new Pusher("APP_KEY", "APP_SECRET", "APP_ID");

$pusher->trigger('test_channel', 'my_event', array('message' => 'hello world') );
var Pusher = require('pusher');

var pusher = new Pusher({
  appId: 'APP_ID',
  key: 'APP_KEY',
  secret: 'APP_SECRET'
});

pusher.trigger('test_channel', 'my_event', {"message": "hello world"});
using PusherServer;
using System.Web.Mvc;
using System.Net;
using Your.Config;

public class HelloWorldController : Controller {
  [httpPost]
  public ActionResult HelloWorld() {
    var pusher = new Pusher(Config.AppId, Config.AppKey, Config.AppSecret);
    var result = pusher.Trigger('test_channel', 'my_event', new { message = "hello world" } );
    return new HttpStatusCodeResult((int)HttpStatusCode.OK);
  }
}
from pusher import Pusher

pusher = Pusher(
  app_id=u'APP_ID',
  key=u'APP_KEY',
  secret=u'APP_SECRET'
)

pusher.trigger(u'test_channel', u'my_event', {u'message': u'hello world'})
package main

import "github.com/pusher/pusher-http-go"

func main(){

  client := pusher.Client{
    AppId: "APP_ID",
    Key: "APP_KEY",
    Secret: "APP_SECRET",
  }

  data := map[string]string{"message": "hello world"}

  client.Trigger("test_channel", "my_event", data)
}
Pusher pusher = new Pusher("APP_ID", "APP_KEY", "APP_SECRET");

pusher.trigger("test_channel", "my_event", Collections.singletonMap("message", "Hello World"));

Note! If you created your app in a different cluster to the default us-east-1, you must configure the cluster option as well. See how.

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, Investigate the JavaScript client library or learn how to excluding event recipients when publishing events.

Have you tried using the search to find what you’re after? If you still have a question then get in touch with us and let us help you out.