pubnub and head.js

2019-05-10 04:18发布

问题:

Has anyone managed to integrate pubnub and head.js properly?

Pubnub expect me to put their script at the bottom of the page with a <div> tag just before it. This ensures that the ` tag is completely loaded by the time the javascript is called last.

The thing is, the rest of my application and pubnub are loaded through head.js so it keeps happening that the JS files are loaded before the pubnub subscribe/publish key and its subscribing/publishing to 'demo' rather than my own area of pubnub...

Is there any example anywhere or way for me to just screw off this <div> element and pass in the credentials directly?

回答1:

Thank you for asking about PubNub with Head.JS usage. I will help you get started quickly. The following is an example of how to us PubNub with Head.JS. First call the head.js() function as you normally would including the PubNub JS Lib. Then call PUBNUB.init({}) function inside the head.js callback:

head.js("/jquery.js", "http://cdn.pubnub.com/pubnub-3.3.1.min.js", function() {
    // CREATE A PUBNUB  OBJECT
    var pubnub = PUBNUB.init({
        'publish_key'   : 'pub-key',
        'subscribe_key' : 'sub-key',
        'ssl'           : false
    });
    pubnub.ready();
    pubnub.subscribe({
        channel : 'abc',
        callback : function(message) { alert(message) }
    });
});

That's it! Pretty simple right? With this code you will be able to use the PubNub Script with Head.JS easily. If there is anything that may be missing from this, please let me know and I will update the code and description.