Lua SDK 3.6.0
Get Code: Source
Depending on the target/platform you're using, you may need some additional setup. Corona and Moai should not need more setup, their environments should provide all we need. All you need is to use the PubNub Lua module in your projects. You can also use the PubNubUtil module, but it's only for convenience.
The pure
Lua platform expects de facto standard lua sockets and crypto modules to be installed and uses the dkjson library that is part of the source tree.
Hello World
require "pubnub"
Note
Always set the UUID
to uniquely identify the user or device that connects to PubNub. This UUID
should be persisted, and should remain unchanged for the lifetime of the user or the device. Not setting the UUID
can significantly impact your billing if your account uses the Monthly Active Users (MAUs) based pricing model, and can also lead to unexpected behavior if you have Presence enabled.
require "pubnub"
require "PubnubUtil" -- has textout()
local pubnub_obj = pubnub.new({
publish_key = "demo",
subscribe_key = "demo",
})
channel = "hello_world"
pubnub_obj:subscribe({
channel = channel,
callback = function(message)
textout(message)
end,
error = function()
textout("Oh no!!! Dropped Connection!")
end,
})
pubnub_obj:publish({
channel = channel,
message = "Hello to the world!",
})
Copy and paste examples
In addition to the Hello World sample code, we also provide some copy and paste snippets of common API functions:
Init
Instantiate a new Pubnub instance. Only the subscribe_key
is mandatory. Also include publish_key
if you intend to publish from this instance, and the secret_key
if you wish to perform PAM administrative operations from this Lua instance.
Important
It is not a best practice to include the secret key in client-side code for security reasons.
When you init with secret_key
, you get root permissions for the Access Manager. With this feature you don't have to grant access to your servers to access channel data. The servers get all access on all channels.
Note
Always set the UUID
to uniquely identify the user or device that connects to PubNub. This UUID
should be persisted, and should remain unchanged for the lifetime of the user or the device. Not setting the UUID
can significantly impact your billing if your account uses the Monthly Active Users (MAUs) based pricing model, and can also lead to unexpected behavior if you have Presence enabled.
Initializing the client
require "pubnub"
local pubnub_obj = pubnub.new({
publish_key = "demo",
subscribe_key = "demo",
ssl = true
})
Time
Call time()
to verify the client connectivity to the origin:
pubnub_obj:time(function(t)
textout("Got timetoken: " .. t)
end)
Subscribe
Subscribe (listen on) 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
})
Publish
Publish a message to a channel:
pubnub_obj:publish({
channel = "demo",
message = "42",
error = function(r)
textout(r)
end
})
Here Now
Get occupancy of who's here now
on the channel by UUID:
Note
Requires you to enable the Presence
add-on for your key. Refer to the How do I enable add-on features for my keys? knowledge base article for details on enabling features.
pubnub_obj:here_now({
channel = "demo",
callback = function(response)
textout(response)
end,
error = function (response)
textout(response)
end
})
Presence
Subscribe to realtime Presence events, such as join
, leave
, and timeout
, by UUID. Setting the presence attribute to a callback will subscribe to presents events on my_channel
:
Note
Requires you to enable the Presence
add-on for your key. Refer to the How do I enable add-on features for my keys? knowledge base article for details on enabling features.
pubnub_obj:subscribe({
channel = "demo",
connect = function()
textout('Connected to channel ')
textout(channel)
end,
callback = function(message)
textout(message)
end,
presence = function(notification)
textout(notification)
end,
error = function()
textout("Oh no!!! Dropped Connection!")
end
})
History
Retrieve published messages from archival storage:
Note
Requires that the Storage and Playback
add-on is enabled for your key. How do I enable add-on features for my keys? - see https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-
pubnub_obj:history({
channel = "demo",
count = 50,
callback = function(response)
textout(response)
end,
error = function (response)
textout(response)
end
})
Unsubscribe
Stop subscribing (listening) to a channel:
pubnub_obj:unsubscribe({
channel = "demo",
})