General API¶
A Backendless application can publish messages to Backendless for subsequent distribution to subscribers. A message must be published to a channel. Backendless supports unlimited number of channels per application. Channels can be used as a filtering mechanism - subscribers see the messages published only to the channel they subscribe to.
There is only one API for all types of message publishing. It supports the following scenarios:
- Basic message publish
- Publishing with headers - can be used for conditional delivery
- Delayed publishing
- Repeated publishing
Publishes a message to the specified channel.
- (void)publishWithChannelName:(NSString * _Nonnull)channelName message:(id _Nonnull)message responseHandler:^(MessageStatus * _Nonnull)responseHandler errorHandler:^(Fault * _Nonnull)errorHandler;
func publish(channelName: String, message: Any, responseHandler: ((MessageStatus) -> Void)!, errorHandler: ((Fault) -> Void)!)
Publishes a message to the specified channel with publish options. Publish options allow to set message headers, publisher ID and subtopic.
- (void)publishWithChannelName:(NSString * _Nonnull)channelName message:(id _Nonnull)message publishOptions:(PublishOptions * _Nonnull)publishOptions responseHandler:^(MessageStatus * _Nonnull)responseHandler errorHandler:^(Fault * _Nonnull)errorHandler;
func publish(channelName: String, message: Any, publishOptions: PublishOptions, responseHandler: ((MessageStatus) -> Void)!, errorHandler: ((Fault) -> Void)!)
Publishes a message to the specified channel with publish and delivery options. Publish options allow to set message headers, publisher ID and subtopic. Delivery options can be used to:
- Publish the message at a later time (delayed publishing).
- Specify that the message should be republished multiple times (repeated publishing).
- (void)publishWithChannelName:(NSString * _Nonnull)channelName message:(id _Nonnull)message publishOptions:(PublishOptions * _Nonnull)publishOptions deliveryOptions:(DeliveryOptions * _Nonnull)deliveryOptions responseHandler:^(MessageStatus * _Nonnull)responseHandler errorHandler:^(Fault * _Nonnull)errorHandler;
func publish(channelName: String, message: Any, publishOptions: PublishOptions, deliveryOptions: DeliveryOptions, responseHandler: ((MessageStatus) -> Void)!, errorHandler: ((Fault) -> Void)!)
where:
Argument | Description |
---|---|
channelName |
Name of the channel to publish the message to. If the channel does not exist, Backendless automatically creates it. |
message |
Object to publish. The object can be of any data type - a primitive value, String, Date, a user-defined complex type, a collection or an array of these types. |
publishOptions |
An instance of PublishOptions . When provided may contain publisher ID (an arbitrary, application-specific string value identifying the publisher), subtopic value and a collection of headers. See the Delayed Publish and Repeated Publish sections for examples. |
deliveryOptions |
An instance of DeliveryOptions . When provided may specify options for message delivery such as: deliver only as a push notification (as opposed to push notification and a pub/sub message), delayed delivery or repeated delivery. See the Delayed Publish and Repeated Publish sections for examples. |
Return value¶
MessageStatus
- A data structure which contains message ID and message status:
@interface MessageStatus : NSObject
@property (nonatomic, copy) NSString *messageId;
@property (nonatomic, copy) NSString * status;
@property (nonatomic, copy) NSString *errorMessage;
@end
@objcMembers open class MessageStatus: NSObject {
open var messageId: String?
open var status: String?
open var errorMessage: String?
}
Supported message statuses are:
PUBLISHED
- message has been successfully publishedSCHEDULED
- message has been put into queue for delivery, it will be published immediately as soon as computing resources become available within BackendlessFAILED
- message publishing failed. See theerrorMessage
property inMessageStatus
for details.
Errors¶
The following errors may occur during the message publishing API call. See the Error Handling section for details on how to retrieve the error code when the server returns an error:
Error Code |
Description |
---|---|
5003 |
Invalid repeatExpiresAt date in delivery options. |
5007 |
User does not have the permission to publish messages |
5030 |
Invalid publishAt date in the delivery options. |
Codeless Reference¶
where:
Argument | Description |
---|---|
channel name |
Name of the channel where a message must be published to. |
subtopic name |
The subtopic of the message allows developers to designate and filter messages by subtopic name. For instance, if a messaging application has a group and a few subgroups, all messages sent to the main group can be marked with this subtopic name which can be used to route messages to the appropriate subgroups depending on the context. |
message |
The contents of the message that must be published to a channel. |
headers |
Message headers is a collection of name/value pairs. A subscriber can set a filter expressed as an SQL "where clause" query (called selector) which Backendless uses to determine whether a message should be delivered to the subscriber. When the query matches the published data in message headers, message is delivered to the corresponding subscriber. For more information see the Conditional Delivery section of this guide. |
publish at |
Must be a Unix Timestamp, which is the number of milliseconds since the Epoch (January 1st, 1970 at UTC). Note that if you want to specify the number of seconds instead of milliseconds, you must multiply the number by 1000 or add three trailing zeroes to your number (e.g. (1681324179 * 1000) or 1681324179000 . |
return result |
When this box is checked, the operation returns an object containing the status of the message delivery and a unique message identifier. |
Returns an object containing the status of the message delivery and a unique message identifier:
// Sample object
{
"errorMessage":null,
"messageId":"message:FCBD8BF1-A45D-4564-B449-3C91D4896987",
"status":"published"
}
For examples refer to the following topics: