简体   繁体   中英

pubnub and head.js

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM