libpusher

libPusher is an open-source Objective-C library for working with the Pusher API. It provides a simple interface to communicate with the Pusher real-time messaging service, making it easier for developers to build real-time features within their applications. This documentation guide aims to provide detailed information on how to use libPusher effectively.

Installation

Requirements

  • iOS 10 or later
  • Xcode 10 or later

Using CocoaPods

1. Add the following line to your Podfile:


pod 'libPusher'

2. Run the command:


pod install

3. Import libPusher in your code as follows:


@import libPusher;

Manual Installation

1. Download the latest version of libPusher from the GitHub repository.

2. Add the libPusher.framework to your Xcode project by dragging and dropping it into your project.

3. In your project settings, navigate to the “General” tab and add libPusher.framework to the “Embedded Binaries” section.

4. Import libPusher in your code as follows:


@import libPusher;

Getting Started

Initialization

1. Import libPusher in your code:


@import libPusher;

2. Create an instance of PTPusher with your Pusher app key:


PTPusher *pusher = [[PTPusher alloc] initWithKey:@"YOUR_APP_KEY"];

3. Connect to the Pusher service:


[pusher connect];

Subscribing to Channels

1. Create an instance of PTPusherChannel:


PTPusherChannel *channel = [pusher subscribeToChannelNamed:@"my-channel"];

2. Implement the event handling methods for the channel:


[channel bindToEventNamed:@"my-event" handleWithBlock:^(PTPusherEvent *event) {
// Handle the event here
}];

3. Call the following method to bind the event handler:


[channel bindToEventNamed:@"my-event"];

Triggering Events

1. Create an instance of PTPusherEvent:


PTPusherEvent *event = [[PTPusherEvent alloc] initWithName:@"my-event" data:@{@"message":@"Hello, World!"}];

2. Trigger the event on a channel:


[pusher triggerEvent:event onChannelNamed:@"my-channel"];

Handling Connection State Changes

1. Implement PTPusherDelegate protocol in your class:


@interface YourClass()

2. Set the delegate of your PTPusher instance:


pusher.delegate = self;

3. Implement the delegate method:


- (void)pusher:(PTPusher *)pusher connectionDidChangeState:(PTPusherConnectionState)state {
// Handle connection state changes here
}

Conclusion

This documentation guide has provided a comprehensive overview on how to utilize libPusher to work with the Pusher real-time messaging service. By following the installation instructions, initializing libPusher, subscribing to channels, triggering events, and handling connection state changes, you should now be equipped with the knowledge to build robust real-time features within your applications using libPusher.