[[toc]]

Subscribing a channel

SKYContainer *container = [SKYContainer defaultContainer];
[container.pubsubClient subscribeTo:@"hello" handler:^(NSDictionary *info) {
    NSString *name = info[@"name"];
    NSLog(@"%@ says hello", name);
}];
let container = SKYContainer.default()
container?.pubsubClient.subscribe(to: "hello", handler: { (info) in
    let name = info?["name"]
    print("\(name) says hello")
})

Publishing to a channel

[container.pubsubClient publishMessage:@{@"name": @"world"} toChannel:@"hello"];
container?.pubsubClient.publishMessage(["name":"world"], toChannel: "hello")

To publish a message to the channel through cloud code, please refer to the Cloud Functions Guide: Calling Skygear API - Pubsub Events.

Unsubscribing a channel

[container.pubsubClient unsubscribe:@"hello"];
container?.pubsubClient.unsubscribe("hello")

Skygear will automatically re-connect on connection drop. Skygear will also re-subscribe all existing handler on connection restore. So in normal case, you don't need to re-subscribe all your handler on re-connect.

Example: PING-PONG

SKYContainer *container = [SKYContainer defaultContainer];

// Pinger
[container.pubsubClient subscribeTo:@"PING" handler:^(NSDictionary *info) {
    NSLog(@"Received a PING");
    [container.pubsubClient publishMessage:nil toChannel:@"PONG"];
}];

// Ponger
[container.pubsubClient subscribeTo:@"PONG" handler:^(NSDictionary *info) {
    NSLog(@"Received a PONG");
    [container.pubsubClient publishMessage:nil toChannel:@"PING"];
}];

// kick start the game
[container.pubsubClient publishMessage:nil toChannel:@"PING"];
let container = SKYContainer.default()
    
// Pinger
container?.pubsubClient.subscribe(to: "PING", handler: { (info) in
    print ("Received a PING")
    container?.pubsubClient.publishMessage(nil, toChannel: "PONG")
})
    
// Ponger
container?.pubsubClient.subscribe(to: "PONG", handler: { (info) in
    print ("Received a PONG")
    container?.pubsubClient.publishMessage(nil, toChannel: "PING")
})
    
// kick start the game
container?.pubsubClient.publishMessage(nil, toChannel: "PING")