iOS Small Talks: PubNub – Solution for the Real-time Communication

We encounter applications running in real-time pretty much everywhere. Any chat or a game uses this technology. Creating communication that is based on the assumption that what is happening in reality should be immediately reflected in the application, is unfortunately neither easy nor cheap. However, for small applications, we don’t need to spend huge money on dedicated infrastructure. PubNub can help us.
How to use PubNub?
I’ve prepared a short demo that shows a simple integration of PubNub with the app.
We don’t need to create an account in order to test PubNub. For the purpose of this example, I used a shared console. It’s available here.
If we want to start publishing and receive messages, we need to initiate an instance of PubNub class using our test keys.
PNConfiguration *configuration = [PNConfiguration configurationWithPublishKey:@"holdapp_demo" subscribeKey:@"holdapp_demo"];
self.client = [PubNub clientWithConfiguration:configuration];
[self.client addListener:self];
where:
@interface ViewController () <PNObjectEventListener>
@property (strong, nonatomic) PubNub *client;
@end
To receive messages incoming to a given channel, we need to subscribe to it, first.
[self.client subscribeToChannels: @[@”holdapp_test_channel”] withPresence:YES];
Now we can go to sending messages. Any valid JSON with a maximum size of 32Kb can be a message.
NSString *message = [NSString stringWithFormat: @"Message:%@" ,self.messageTextField.text];
[self.client publish: @{@"message": message} toChannel:@”holdapp_test_channel” withCompletion:^(PNPublishStatus *status) {}];
Receiving messages is possible after implementing methods of the PNObjectEventListener protocol, as presented in the example project.
The whole process of posting and receiving messages is very simple.
We don’t need to create a specific channel. Sending messages to a given channel is all we have to do. All devices that subscribed to a particular channel will receive our message.
In addition to basic sending and receiving messages, we have access to many other interesting functions, such as:
- keeping track of online statuses of specific users,
- accessing the history of posted messages,
- Access Manager, which provides security of data exchange,
- sending push notifications,
- Stream Controller, facilitating managing thousands of channels,
- Analytics.
PubNub provides SDK for dozens of platforms, including iOS and Android. For those interested in creating an application, there was published a series of tutorials named University of PubNub. They are useful to both beginner developers and those who want to build a complex application.
The advantage of PubNub is that if our application doesn’t exceed 100 users per day or 100 million sent messages per month, we can access all extras for free. Unfortunately, this condition is met only by small applications and exceeding these numbers posts substantial costs.
Integrating PubNub with an application is really simple. Having written a few lines of code, we can send and receive messages in real-time, and the communication process is being performed really fast. If you want to try to develop an application running in real-time, PubNub is an option worth considering.