LuaLua SDK 3.6.0



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.

require "pubnub"
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!",
})
In addition to the Hello World sample code, we also provide some copy and paste snippets of common API functions:

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.

 

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.

require "pubnub"

local pubnub_obj = pubnub.new({
    publish_key = "demo",
    subscribe_key = "demo",
    ssl = true
})
Call time() to verify the client connectivity to the origin:
pubnub_obj:time(function(t)
    textout("Got time token: " .. t)
end)
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 a message to a channel:
pubnub_obj:publish({
    channel = "demo",
    message = "42",
    error = function(r)
        textout(r)
    end
})
Get occupancy of who's here now on the channel by UUID:
Requires that the Presence add-on is enabled for your key. How do I enable add-on features for my keys? - see http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys
pubnub_obj:here_now({
	channel = "demo",
	callback = function(response)
		textout(response)
	end,
	error = function (response)
		textout(response)
	end
})
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:
Requires that the Presence add-on is enabled for your key. How do I enable add-on features for my keys? - see http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys
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
})
Retrieve published messages from archival storage:
Requires that the Storage and Playback add-on is enabled for your key. How do I enable add-on features for my keys? - see http://www.pubnub.com/knowledge-base/discussion/644/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
})
pubnub_obj:unsubscribe({
	channel = "demo",
})