javascript

A Simple Example: Hello World!

You can subscribe and publish a message like so:
 <script src="http://cdn.pubnub.com/pubnub.min.js"></script> (1)
 
 <script>
 function publish() {
 
     pubnub = PUBNUB.init({                                  (2)
         publish_key   : 'demo',
         subscribe_key : 'demo'
     })
 
     pubnub.subscribe({                                      (3)
         channel : "hello_world",
         message : function(message,env,channel){
           document.getElementById('text').innerHTML =
           "Message Received." + '<br>' +
           "Channel: " + channel + '<br>' +
           "Message: " + JSON.stringify(message)  + '<br>' +
           "Raw Envelope: " + JSON.stringify(env) + '<br>'
         },
         connect: pub
     })
 
     function pub() {
        pubnub.publish({                                     (4)
             channel : "hello_world",
             message : "Hello from PubNub Docs!",
             callback: function(m){ console.log(m) }
        })
     }
 };</script>
1 Include the SDK into your application code.
2 Initialize the library with your publish and subscribe keys.
3 Optionally, subscribe to the channel to begin receiving messages.
4 Publish a message to the channel for the world to see.
Run The Code
See hello_world channel output here.
More Fun Awaits:

Look at the Quick Start.
Try out a Tutorial.
Find out more about PubNub Features.

Where do I get the code?

The simplest way to build an application is to include the JavaScript SDK into your application using the following links:

https://cdn.pubnub.com/pubnub.js (Latest version)
https://cdn.pubnub.com/pubnub.min.js (Minified latest version)
https://cdn.pubnub.com/pubnub-crypto.min.js (For secure apps - deprecated)

Just insert the <script> tag before the closing HTML </body> tag:

<html>
   <body>
      <script src="http://cdn.pubnub.com/pubnub.min.js"></script>
   </body>
</html>

For production environments, static JavaScript libraries are always available for specific versions of the SDK in both minified and non-minified form factor. The list below is provided as an example, older versions are also available and follow the same naming convention pubnub-<version-string>.js.

GitHub

Or you can git clone the JavaScript SDK repository with the following command:

git clone https://github.com/pubnub/javascript.git

How do I get started?

All you need is:

  • Any Mobile Browser.

  • Any Desktop Browser.

  • Your real-time application.

Then see the Quickstart .
Or try a Tutorial .

Also see guides for:
AngularJS
Socket.IO
Websockets

Checkout working demos here.

What other JavaScript based SDKs are supported?

PubNub supports an amazing array of platforms and frameworks with easy to use APIs for Mobile, Browser, Desktop and Server. Select one of the SDKs for JavaScript below to find out more.

Table 1. PubNub JavaScript based SDKs to suit various application requirements.
PubNub SDK Description

pubnub javascript sdk

Web: supports all Mobile and Desktop Browsers. It is a good idea to start with the web library as it supports all legacy, modern and mobile web browsers.

pubnub html5 sdk

Modern: is a light-weight build with support for only the latest Browsers. The modern library is CommonJS and HTML5 WebWorker ready and is lighting fast.

pubnub smart-tv sdk

Smart-TV: provides real-time support for smart televisions platforms such as LG, Philips, Samsung, Sony, Westinghouse and Vizio.

pubnub titanium sdk

Titanium: provides real-time support for the Appcelerator Titanium platform for mobile, desktop and web technologies.

pubnub node-js sdk

Node.JS: build (CommonJS) server-side real-time applications.

pubnub socket.io sdk

Socket.IO: We enhanced Socket.IO with PubNub. Faster JavaScript, Smaller Footprint, Faster Cloud Network and Socket.IO with PubNub. This library does not require a Node.JS back-end. This means your code is lean and simple giving you extra time to build your app. The updated JS payload has been optimized for Mobile Apps; which means excellent performance for laptops too. See a getting started guide here

pubnub phonegap sdk

Phone Gap: Build real-time apps with platform support for mobile devices that utilize JavaScript, HTML5 and CSS3.

pubnub sencha sdk

Sencha: Sencha provides an HTML5 product suite that provides mobile developers with the frameworks and tools they need to build touch-based apps in a single, integrated package. Now totally real-time with PubNub.

pubnub webos sdk

WebOS: provides real-time support for smart TVs and mobile devices running Open WebOS.

pubnub impactjs sdk

Impact.JS: The Awesomest Way to Create Even More Awesome HTML5 Games just became the MOST Awesomest with the addition of our PubNub client for Impactjs.

pubnub monkey sdk

Monkey: Build real-time next-generation games programming on multiple platforms with the greatest of ease. Monkey games can be run on potentially hundreds of different devices - including mobile phones, tablets, desktop computers and even video game consoles.

pubnub qooxdoo sdk

Qooxdoo: With our qooxdoo client you can build real-time, interactive applications, native-like apps for mobile devices, light-weight single-page oriented web applications or even applications to run outside the browser.

pubnub webrtc sdk

WebRTC: Our WebRTC API will perform signaling between your users to allow them to connect with a RTCPeerConnection. From there you can use the PubNub API to enhance your peer application with features such as presence and history. PubNub Presence will allow you to find what users are connected to your application and give you a phonebook of people to connect to. You can also use history to see what connections you have made and reconnect to people from the past.