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

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.

Learn more

How to Send iOS Notifications in Different Languages? Guide to Dynamic Localization

The best way to set a connection between a user, provider, and an app? System notifications. They allow users to get the latest news in no time. This solution is easy to implement in apps dedicated to one market. But it gets complicated when the messages must be displayed in many languages. In such cases, the dynamic localization of remote notifications can be a real game-changer.

Read more

Promises on the Example of PromiseKit in Objective-C

Sooner or later every programmer encounters the problem of synchronous execution of certain actions. For example: get user information from the API, parse server response, save data to the database, update the view, and many others. To make it even more, at some of these stages you still need to deal with error handling. What should you do exactly?

Read more

Bitrise Tests Made Easier: Update JIRA Issues with Build Number and Forget About Delays in QA Testing

When you trigger Bitrise build with changes and forget to tell QA specialists some essential information, there are two likely scenarios. You waste time waiting for the update from the tester, only you don’t know it’s never coming. Or you get so many questions about builds that you can’t keep up. Sounds familiar? If so, let us tell you about the JIRA issue update that keeps workflow in order.

Read more

Project estimation

Let us know what product you want to build and how we can help you.

Why choose us?

Logo Mobile Trends Awards

Mobile Trends Awards 2021

Winning app in
EVERYDAY LIFE category

Nagroda Legalnych Bukmacherów

Legal Bookmakers Award 2019

Best Mobile App

Mobile Trends Awards logo

Mobile Trends Awards 2020

Nomination in SPORTS & RECREATION category

20

client reviews

Clutch logo