Publish/Subscribe API for PubNub Cocoa Objective-C SDK
Publish
The publish
function is used to send a message to all subscribers of a channel. To publish a message you must first specify a valid publishKey
at initialization. A successfully published message is replicated across the PubNub Real-Time Network and sent simultaneously to all subscribed clients on a channel.
Messages in transit can be secured from potential eavesdroppers with SSL/TLS by setting ssl to true during initialization.
Publish Anytime
It's not required to be subscribed to a channel in order to publish to that channel.
Message Data
The message argument can contain any JSON serializable data, including: Objects, Arrays, Ints and Strings. data
should not contain special Cocoa classes or functions as these will not serialize. String content can include any single-byte or multi-byte UTF-8 character.
Don't JSON serialize
It is important to note that you should not JSON serialize when sending signals/messages via PUBNUB. Why? Because the serialization is done for you automatically. Instead just pass the full object as the message payload. PubNub takes care of everything for you.
Message Size
The maximum number of characters per message is 32 KiB by default. The maximum message size is based on the final escaped character count, including the channel name. An ideal message size is under 1800 bytes which allows a message to be compressed and sent using single IP datagram (1.5 KiB) providing optimal network performance.
If the message you publish exceeds the configured size, you will receive the following message:
Message Too Large Error
["PUBLISHED",[0,"Message Too Large","13524237335750949"]]
For further details, check Calculating Message Payload Size Before Publish.
Message Publish Rate
Messages can be published as fast as bandwidth conditions will allow. There is a soft limit based on max throughput since messages will be discarded if the subscriber can't keep pace with the publisher.
For example, if 200 messages are published simultaneously before a subscriber has had a chance to receive any messages, the subscriber may not receive the first 100 messages because the message queue has a limit of only 100 messages stored in memory.
Publishing to Multiple Channels
It is not possible to publish a message to multiple channels simultaneously. The message must be published to one channel at a time.
Publishing Messages Reliably
There are some best practices to ensure messages are delivered when publishing to a channel:
- Publish to any given channel in a serial manner (not concurrently).
- Check that the return code is success (for example,
[1,"Sent","136074940..."]
) - Publish the next message only after receiving a success return code.
- If a failure code is returned (
[0,"blah","<timetoken>"]
), retry the publish. - Avoid exceeding the in-memory queue's capacity of 100 messages. An overflow situation (aka missed messages) can occur if slow subscribers fail to keep up with the publish pace in a given period of time.
- Throttle publish bursts in accordance with your app's latency needs, for example, Publish no faster than 5 msgs per second to any one channel.
Publishing Compressed Messages
Message compression lets you send the message payload as the compressed body of an HTTP POST call.
Every PubNub SDK supports sending messages using the publish()
call in one or more of the following ways:
- Uncompressed, using HTTP GET (message sent in the URI)
- Uncompressed, using HTTP POST (message sent in the body)
- Compressed, using HTTP POST (compressed message sent in the body)
This section outlines what compressing a message means, and how to use compressed messages to your advantage.
Suppport for compressed messages
Currently, the C and Objective-C SDKs support compressed messages.
Message compression can be helpful if you want to send data exceeding the default 32 KiB message size limit, or use bandwidth more efficiently. Compressing messages is useful for scenarios that include high channel occupancy and quick exchange of information like ride hailing apps or multiplayer games.
Compression Trade-offs
Small messages can expand
Compressed messages generally have a smaller size, and can be delivered faster, but only if the original message is over 1 KiB. If you compress a signal (whose size is limited to 64 bytes), the compressed payload exceeds the signal's initial uncompressed size.
CPU overhead can increase
While a smaller payload size is an advantage, working with compressed messages uses more CPU time than working with uncompressed messages. CPU time is required to compress the message on the sending client, and again to decompress the message on the receiving client. Efficient resource management is especially important on mobile devices, where increased usage affects battery life. Carefully consider the balance of lower bandwidth and higher speed versus any increased CPU usage.
Using Compression
Compression methods and support vary between SDKs. If the receiving SDK doesn't support the sender's compression method, or even if it doesn't support compression at all, the PubNub server automatically changes the compressed message's format so that it is understandable to the recipient. No action is necessary from you.
Messages are not compressed by default; you must always explicitly specify that you want to use message compression. Refer to the code below for an example of sending a compressed message.
[self.client publish:@{@"message": @"This message will be compressed"}
toChannel:@"channel_name" compressed:YES
withCompletion:^(PNPublishStatus *status) {
if (!status.isError) {
// Message successfully published to specified channel.
} else {
// Handle error.
}
}]
Method(s)
To Publish a message
you can use the following method(s) in the Cocoa SDK:
Publish a message with block
- (void)publish:(id)message
toChannel:(NSString *)channel
withCompletion:(nullable PNPublishCompletionBlock)block;
Parameter | Type | Required | Description |
---|---|---|---|
message | id | Yes | Reference on Foundation object ( NSString , NSNumber , NSArray , NSDictionary ) which will be published. |
channel | NSString | Yes | Reference on name of the channel to which message should be published. |
block | PNPublishCompletionBlock | No | Publish processing completion block which pass only one argument - request processing status to report about how data pushing was successful or not. |
Publish a message with compression and block
- (void)publish:(id)message
toChannel:(NSString *)channel
compressed:(BOOL)compressed
withCompletion:(nullable PNPublishCompletionBlock)block;
Parameter | Type | Required | Description |
---|---|---|---|
message | id | Yes | Reference on Foundation object (NSString , NSNumber , NSArray , NSDictionary ) which will be published. |
channel | NSString | Yes | Reference on name of the channel to which message should be published. |
compressed | BOOL | Yes | Whether message should be compressed and sent with request body instead of URI part. Compression useful in case if large data should be published, in another case it will lead to packet size grow. |
block | PNPublishCompletionBlock | No | Publish processing completion block which pass only one argument - request processing status to report about how data pushing was successful or not. |
Publish a message with storage and block
- (void)publish:(id)message
toChannel:(NSString *)channel
storeInHistory:(BOOL)shouldStore
withCompletion:(nullable PNPublishCompletionBlock)block;
Parameter | Type | Required | Description |
---|---|---|---|
message | id | Yes | Reference on Foundation object (NSString , NSNumber , NSArray ,NSDictionary ) which will be published. |
channel | NSString | Yes | Reference on name of the channel to which message should be published. |
shouldStore | BOOL | Yes | With NO this message later won't be fetched using Message Persistence API. |
block | PNPublishCompletionBlock | No | Publish processing completion block which pass only one argument - request processing status to report about how data pushing was successful or not. |
Publish a message with storage, compression, and block
- (void)publish:(id)message
toChannel:(NSString *)channel
storeInHistory:(BOOL)shouldStore
compressed:(BOOL)compressed
withCompletion:(nullable PNPublishCompletionBlock)block;
Parameter | Type | Required | Description |
---|---|---|---|
message | id | Yes | Reference on Foundation object (NSString , NSNumber , NSArray ,NSDictionary ) which will be published. |
channel | NSString | Yes | Reference on name of the channel to which message should be published. |
shouldStore | BOOL | Yes | With NO this message later won't be fetched using Message Persistence API. |
compressed | BOOL | Yes | Compression useful in case if large data should be published, in another case it will lead to packet size grow. |
block | PNPublishCompletionBlock | No | Publish processing completion block which pass only one argument - request processing status to report about how data pushing was successful or not. |
Publish a message with payload and block
- (void)publish:(nullable id)message
toChannel:(NSString *)channel
mobilePushPayload:(nullable NSDictionary<NSString *, id> *)payloads
withCompletion:(nullable PNPublishCompletionBlock)block;
Parameter | Type | Required | Description |
---|---|---|---|
message | id | No | Reference on Foundation object (NSString , NSNumber , NSArray , NSDictionary ) which will be published. |
channel | NSString | Yes | Reference on name of the channel to which message should be published. |
payloads | NSDictionary | No | Dictionary with payloads for different vendors (Apple with apns key and Google with gcm ). |
block | PNPublishCompletionBlock | No | Publish processing completion block which pass only one argument - request processing status to report about how data pushing was successful or not. |
Publish a message with payload, compression, and block
- (void)publish:(nullable id)message
toChannel:(NSString *)channel
mobilePushPayload:(nullable NSDictionary<NSString *, id> *)payloads
compressed:(BOOL)compressed
withCompletion:(nullable PNPublishCompletionBlock)block;
Parameter | Type | Required | Description |
---|---|---|---|
message | id | No | Reference on Foundation object (NSString , NSNumber , NSArray , NSDictionary ) which will be published. |
channel | NSString | Yes | Reference on name of the channel to which message should be published. |
payloads | NSDictionary | Yes | Dictionary with payloads for different vendors (Apple with apns key and Google with gcm ). |
compressed | BOOL | Yes | Compression useful in case if large data should be published, in another case it will lead to packet size grow. |
block | PNPublishCompletionBlock | No | Publish processing completion block which pass only one argument - request processing status to report about how data pushing was successful or not. |
Publish a message with payload, storage, and block
- (void)publish:(nullable id)message
toChannel:(NSString *)channel
mobilePushPayload:(nullable NSDictionary<NSString *, id> *)payloads
storeInHistory:(BOOL)shouldStore
withCompletion:(nullable PNPublishCompletionBlock)block;
Parameter | Type | Required | Description |
---|---|---|---|
message | id | No | Reference on Foundation object (NSString , NSNumber , NSArray ,NSDictionary ) which will be published. |
channel | NSString | Yes | Reference on name of the channel to which message should be published. |
payloads | NSDictionary | No | Dictionary with payloads for different vendors (Apple with apns key and Google with gcm ). |
shouldStore | BOOL | Yes | With NO this message later won't be fetched using Message Persistence API. |
block | PNPublishCompletionBlock | No | Publish processing completion block which pass only one argument - request processing status to report about how data pushing was successful or not. |
Publish a message with payload, storage, compression, and block
- (void)publish:(nullable id)message
toChannel:(NSString *)channel
mobilePushPayload:(nullable NSDictionary<NSString *, id> *)payloads
storeInHistory:(BOOL)shouldStore
compressed:(BOOL)compressed
withCompletion:(nullable PNPublishCompletionBlock)block;
Parameter | Type | Required | Description |
---|---|---|---|
message | id | No | Reference on Foundation object (NSString , NSNumber , NSArray ,NSDictionary ) which will be published. |
channel | NSString | Yes | Reference on name of the channel to which message should be published. |
payloads | NSDictionary | No | Dictionary with payloads for different vendors (Apple with "apns" key and Google with "gcm"). |
shouldStore | BOOL | Yes | With NO this message later won't be fetched using Message Persistence API. |
compressed | BOOL | Yes | Compression useful in case if large data should be published, in another case it will lead to packet size grow. |
block | PNPublishCompletionBlock | No | Publish processing completion block which pass only one argument - request processing status to report about how data pushing was successful or not. |
Publish a message with metadata and block
- (void)publish:(id)message
toChannel:(NSString *)channel
withMetadata:(nullable NSDictionary<NSString *, id> *)metadata
completion:(nullable PNPublishCompletionBlock)block;
Parameter | Type | Required | Description |
---|---|---|---|
message | id | Yes | The message may be any valid foundation object (String , NSNumber , Array , Dictionary ). |
channel | NSString | Yes | Specifies channel name to publish messages to. |
metadata | NSDictionary | No | NSDictionary with values which should be used by PubNub service to filter messages. |
block | PNPublishCompletionBlock | No | The completion block which will be called when the processing is complete, has one argument: - request status reports the publish was successful or not (errorData contains error information in case of failure). |
Publish a message with compression, metadata, and block
- (void)publish:(id)message
toChannel:(NSString *)channel
compressed:(BOOL)compressed
withMetadata:(nullable NSDictionary<NSString *, id> *)metadata
completion:(nullable PNPublishCompletionBlock)block;
Parameter | Type | Required | Description |
---|---|---|---|
message | id | Yes | The message may be any valid foundation object (String , NSNumber , Array , Dictionary ). |
channel | NSString | Yes | Specifies channel name to publish messages to. |
compressed | BOOL | Yes | If true the message will be compressed and sent with request body instead of the URI. Compression useful in case of large data, in another cases it will increase the packet size. |
metadata | NSDictionary | No | NSDictionary with values which should be used by PubNub service to filter messages. |
block | PNPublishCompletionBlock | No | The completion block which will be called when the processing is complete, has one argument: - request status reports the publish was successful or not (errorData contains error information in case of failure). |
Publish a message with storage, metadata, and block
- (void)publish:(id)message
toChannel:(NSString *)channel
storeInHistory:(BOOL)shouldStore
withMetadata:(nullable NSDictionary<NSString *, id> *)metadata
completion:(nullable PNPublishCompletionBlock)block;
Parameter | Type | Required | Description |
---|---|---|---|
message | id | Yes | The message may be any valid foundation object (String , NSNumber , Array , Dictionary ). |
channel | NSString | Yes | Specifies channel name to publish messages to. |
shouldStore | BOOL | Yes | If false the messages will not be stored in Message Persistence, default true . |
metadata | NSDictionary | No | NSDictionary with values which should be used by PubNub service to filter messages. |
block | PNPublishCompletionBlock | No | The completion block which will be called when the processing is complete, has one argument: - request status reports the publish was successful or not (errorData contains error information in case of failure). |