Skip to content

Instantly share code, notes, and snippets.

@brianpattison
Created July 25, 2012 08:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianpattison/3175033 to your computer and use it in GitHub Desktop.
Save brianpattison/3175033 to your computer and use it in GitHub Desktop.
Appcelerator Titanium + Pusher WebView
var pusherClient = require('/lib/pusher/pusher_client').create({
debug: true,
key: 'YOUR KEY'
});
pusherClient.subscribe('channel');
pusherClient.bind('event_name', function(data) {
// Do something
});
<!DOCTYPE html>
<head>
<title>Pusher Test</title>
<script src="http://js.pusher.com/1.11/pusher.min.js" type="text/javascript"></script>
<script type="text/javascript">
var pusher, channelName, pusherChannel, bound = [];
function load(debug, key) {
if (debug === true) {
Pusher.log = function(message) { Ti.API.debug('[Pusher] ' + message); };
}
pusher = new Pusher(key);
}
function subscribe(channel) {
if (channelName === channel) {
return;
} else if (typeof channelName === 'string') {
pusher.unsubscribe(channelName);
}
channelName = channel;
pusherChannel = pusher.subscribe(channelName);
bound = [];
pusherChannel.bind('pusher:subscription_succeeded', function() {
Ti.API.info('Subscribed to ' + channelName + ' successfully!');
});
pusherChannel.bind('pusher:subscription_error', function(status) {
Ti.API.warn('Failed to subscribe to ' + channelName + '. Retrying...');
if (status === 408 || status === 503) subscribe(channelName);
});
}
function bind(eventName) {
if (bound.indexOf(eventName) < 0) {
bound.push(eventName);
pusherChannel.bind(eventName, function(data) {
Ti.App.fireEvent(eventName, data);
});
}
}
</script>
</head>
var Ember = require('/lib/em_ti/ember-runtime'),
WebView = require('/lib/em_ti/ui/web_view'),
Window = require('/lib/em_ti/ui/window');
var PusherWebView = WebView.extend({
top: 0,
left: 0,
height: 1,
width: 1,
url: '/lib/pusher/pusher.html',
load: function() {
this.evalJS("load(" + this.get('debug') + ", '" + this.get('key') + "');");
}
});
var PusherClient = Ember.Object.extend({
debug: true,
key: null,
bound: [],
init: function() {
this._super();
var debug = this.get('debug'), key = this.get('key');
if (Ember.none(key)) {
Ti.API.warn('[Pusher] You must set the key property for your Pusher account.');
return;
} else {
var webView = PusherWebView.create({
debug: debug,
key: key
});
this.set('webView', webView);
var win = Window.create({
top: -1,
left: 0,
height: 1,
width: 1
});
win.add(webView);
win.open();
}
},
bind: function(eventName, callback) {
if (this.get('bound').indexOf(eventName) < 0) {
this.bound.push(eventName);
Ti.App.addEventListener(eventName, callback);
}
var webView = this.get('webView');
webView.evalJS("bind('" + eventName + "');");
},
subscribe: function(channelName) {
var webView = this.get('webView');
webView.evalJS("subscribe('" + channelName + "');");
}
});
module.exports = PusherClient;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment