iOS quick start

Getting started with Channels 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, app_secret and app_cluster.

Install the library

Install using CocoaPods.

1
pod 'PusherSwift'

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

  • iOS
  • Objective-C
1
import PusherSwift

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

  • iOS
  • Objective-C
1
2
3
4
5
6
let options = PusherClientOptions(
  host: .cluster("YOUR_CLUSTER")
)

let pusher = Pusher(key: "YOUR_APP_KEY", options: options)
pusher.connect()

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

Subscribe to a public channel

  • iOS
  • Objective-C
1
let myChannel = pusher.subscribe("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.

  • iOS
  • Objective-C
1
2
3
4
5
6
7
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)
    }
  }
})

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 with channels. Learn how to exclude event recipients when publishing events.