Get Code: CDN
Latest stable version
Latest stable minified version
Development version
Get Code: Source
Hello World
You must include the PubNub JavaScript SDK in your code before initializing the client
Pubnub Flash SDK is a proxy/wrapper, it doesn't contain any logic for methods, but delegates calls to JavaScript SDK.
In Pubnub Flash SDK you should specify each argument explicitly, even if you don't intend use it. Alternatively you can use '...args' statement. |
- Add swc file to you Flash project:To use Pubnub, simply copy the
pubnublib.swc
file into your project's libs directory fromhttps://github.com/pubnub/flash/tree/master/pubnublib/bin
- Add
as2js
proxy helper and JavaScript SDK to your html page<html> <body> <script src="http://cdn.pubnub.com/pubnub-3.7.12.min.js"></script> <script src="http://cdn.pubnub.com/pubnub-as2js-proxy.min.js"></script> </body> </html>
Example
var pubnub:PubNub = new PubNub({ publish_key: 'demo', subscribe_key: 'demo' }); trace("Subscribing..."); pubnub.subscribe({ channel: "hello_world", message: function (message:Object, envelope:Object, groupOrChannel:String, time:Number, channel:String):void { trace( "Message Received." + "\n" + "Group or Channel: " + groupOrChannel + "\n" + "Message: " + JSON.stringify(message) + "\n" + "Raw Envelope: " + JSON.stringify(envelope) + "\n" + "Channel: " + JSON.stringify(channel) ) }, connect: publish }); function publish():void { trace("Since we’re publishing on subscribe connectEvent, we’re sure we’ll receive the following publish."); pubnub.publish({ channel: "hello_world", message: "Hello from PubNub Docs!", callback: function (message:Object):void { trace(message) } }) }
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 Flash instance.It is not a best practice to include the secret key in client-side code for security reasons. When you init with |
var pubnub:PubNub = new PubNub({ publish_key: "my_pubkey", subscribe_key: "my_subkey" });
Time
Call
time()
to verify the client connectivity to the origin:pubnub.time(function (time:String):void { trace(time); });
Subscribe
Subscribe (listen on) a channel (it's async!):
pubnub.subscribe({ channel: 'my_channel', message: function (message:Object, envelope:Object, groupOrChannel:String, time:Number, channel:String):void { trace(JSON.stringify(message)) }, error: function (error:Object):void { trace(JSON.stringify(error)) } });
Publish
Publish a message to a channel:
pubnub.publish({ channel: 'my_channel', message: 'Hello from the PubNub Flash SDK!', callback: function (message:Object):void { trace(JSON.stringify(message)) } });
Here Now
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.here_now({ channel: 'my_channel', callback: function (message:Object):void { trace(JSON.stringify(message)) } });
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
: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.subscribe({ channel: 'my_channel', message: function (message:Object, envelope:Object, groupOrChannel:String, time:Number, channel:String):void { trace(JSON.stringify(message)) }, presence: function (message:Object, envelope:Object, groupOrChannel:String, time:Number, channel:String):void { trace(JSON.stringify(message)); } });
History
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.history({ channel: 'history_channel', count: 100, // 100 is the default reverse: false, // false is the default callback: function (message:Object):void { trace(JSON.stringify(message)) } });
Unsubscribe
Stop subscribing (listening) to a channel.
pubnub.unsubscribe({ channel : 'my_channel' });