SelectPubNub Titanium SDK 3.16.5




 
var pubnub = require('pubnub');
var pubnub = PUBNUB({
    publish_key       : 'demo',
    subscribe_key     : 'demo',
    ssl               : false,
    native_tcp_socket : false,
    origin            : 'pubsub.pubnub.com'
});

pubnub.publish({
    channel : "hello_world",
    message : "Hi."
})

pubnub.subscribe({
    channel  : "hello_world",
    message : function(message) {
        Ti.API.log(message)
    }
})
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 Titanium 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.

// Initialize the instance

var pubnub = PUBNUB.init({
    publish_key: 'demo',
    subscribe_key: '',
    error: function (error) {
		Titanium.API.info('Error:', error);
	}
})
Call time() to verify the client connectivity to the origin:
// At the tone PubNub time will be...

pubnub.time(
	function(time){
		Titanium.API.info(time)
	}
);
Subscribe (listen on) a channel (it's async!):
// Subscribe to a channel

pubnub.subscribe({
    channel: 'my_channel',
	message: function(m){
		Titanium.API.info(m)
	},
	error: function (error) {
		// Handle error here
		Titanium.API.info(JSON.stringify(error));
	}
});
Publish a message to a channel:
pubnub.publish({
	channel: 'my_channel',
	message: 'Hello from the PubNub Javascript SDK!',
	callback : function(m){
		Titanium.API.info(m)
	}
});
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
// Get List of Occupants and Occupancy Count.
 
pubnub.here_now({
	channel : 'my_channel',
	callback : function(m){
		Titanium.API.info(m)
	}
});
 
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",
	presence: function(m){
		Titanium.API.info(m)
	},
	message: function(m){
		Titanium.API.info(m)
	}
});
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',
    callback: function(m){
        Titanium.API.info(JSON.stringify(m))
    },
    count: 100, // 100 is the default
    reverse: false // false is the default
});
Stop subscribing (listening) to a channel.
// Unsubscribe from 'my_channel'

pubnub.unsubscribe({
	channel : 'my_channel',
});