How do I load the JavaScript SDK on-demand using require.js?

There are several ways you can use require.js* (as well as other alternatives). Here's one way:

  • Place the require.js library in ./js/lib
  • In your main HTML <head> section, add the following tag: ~~~ ~~~
  • Place main.js in the js folder and include this code: ~~~
    require.config({ paths: { "pubnub"=> "libs/pubnub-3.6.4" }, shim: { "pubnub"=> { exports: "PUBNUB" } }}); ~~~
  • To load PubNub and subscribe to a channel (in main.js or elsewhere): ~~~ require(['pubnub'], function(PUBNUB){ var pubnub = PUBNUB.init({}); pubnub.ready(); pubnub.subscribe({}); }); ~~~

You can also have an external configuration tool.