Publish/Subscribe API for PubNub Lua 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 publish_key
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 Lua classes or functions as these will not serialize. String content can include any single-byte or multi-byte UTF-8 character.
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 the support article.
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 exmple,
[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.
Method(s)
To Publish a message
you can use the following method(s) in the Lua SDK:
pubnub_obj:publish(params)
Parameter | Type | Required | Description |
---|---|---|---|
params | table | Yes | Table of publish parameters. See Publish Parameters for more details. |
Publish Parameters
Properties | Type | Required | Default | Description |
---|---|---|---|---|
channel | string | Yes | none | The channel to publish to. |
message | string | Yes | none | The message to publish. |
callback | function(r) | Optional | function(r) end | The function to call on success with result. |
error | function(r) | Optional | function(r) end | The function to call on failure, with result. |
Basic Usage
Publish a message to a channel
pubnub_obj:publish({
channel = channel,
message = text,
callback = function(r) --textout(r)
end,
error = function(r) textout(r)
end
})
Subscribe to the channel
Before running the above publish example, either using the Debug Console or in a separate script running in a separate terminal window, subscribe to the same channel that is being published to.
Subscribe
This function causes the client to create an open TCP socket to the PubNub Real-Time Network and begin listening for messages on a specified channel
. To subscribe to a channel
the client must send the appropriate subscribe_key
at initialization. By default a newly subscribed client will only receive messages published to the channel after the subscribe()
call completes.
Unsubscribing from all channels
Unsubscribing from all channels, and then subscribing to a new channel Y is not the same as subscribing to channel Y and then unsubscribing from the previously-subscribed channel(s). Unsubscribing from all channels resets the last-received timetoken
and thus, there could be some gaps in the subscription that may lead to message loss.
Method(s)
To Subscribe to a channel
you can use the following method(s) in the Lua SDK:
pubnub_obj:subscribe(params)
Parameter | Type | Required | Description |
---|---|---|---|
params | table | Yes | Table of subscribe parameters. See Subscribe Parameters for more details. |
Subscribe Parameters
Properties | Type | Required | Default | Description |
---|---|---|---|---|
channel | string | Yes | none | The channel to subscribe to - can be a comma-separated list of channels. |
callback | function(msg) | Yes | no | The function to call on each received message. |
error | function(r) | Optional | function(r) end | The function to call on failure, with result. |
connect | function() | Optional | function() end | The function to call on connecting to PubNub (before first message). |
reconnect | function() | Optional | funcition() end | Call if connection lost and then re-established. |
presence | boolean | Optional | false | Whether to subscribe to the presence channel(s) for given channel(s). |
timeout | integer | Optional | SUB_TIMEOUT (310) | The number of seconds to wait for a response to subscribe. |
Basic Usage
Subscribe to a channel:
pubnub_obj:subscribe({
channel = "demo",
connect = function()
textout('Connected to channel ')
textout(channel)
end,
callback = function(message)
textout(message)
end,
error = function()
textout("Oh no!!! Dropped Connection!")
end
})
Unsubscribe
When subscribed to a single channel, this function causes the client to issue a leave
from the channel
and close any open socket to the PubNub Network. For multiplexed channels, the specified channel
(s) will be removed and the socket remains open until there are no more channels remaining in the list.
Unsubscribing from all channels
Unsubscribing from all channels, and then subscribing to a new channel Y is not the same as subscribing to channel Y and then unsubscribing from the previously-subscribed channel(s). Unsubscribing from all channels resets the last-received timetoken
and thus, there could be some gaps in the subscription that may lead to message loss.
Method(s)
To Unsubscribe from a channel
you can use the following method(s) in the Lua SDK:
pubnub_obj:unsubscribe(params)
Parameter | Type | Required | Description |
---|---|---|---|
params | table | Yes | Table of unsubscribe parameters - channel(s) to unsubscribe from. See Unsubscribe Parameters for more details. |
Unsubscribe Parameters
Properties | Type | Required | Default | Description |
---|---|---|---|---|
channel | string | Yes | none | The channel to unsubscribe from - can be a comma-separated list of channels. |
Basic Usage
Unsubscribe from a channel:
pubnub_obj:unsubscribe({
channel = "demo",
})