Switch branches/tags
Nothing to show
Find file History
Pull request Compare This branch is 1 commit ahead, 1243 commits behind pubnub:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


PubNub WebSocket Emulation

PubNub offers full RFC 6455 Support for WebSocket Client Specification. PubNub WebSockets enables any browser (modern or not) to support the HTML5 WebSocket standard APIs. Use the WebSocket Client Directly in your Browser that Now you can use new WebSocket anywhere!

Here is a quick example:

var socket = new WebSocket('wss://pubsub.pubnub.com/PUB/SUB/CHANNEL')

The following example opens a new WebSocket in WSS Secure Socket Mode with full 2048 Bit SSL Encryption.

<!-- Import PubNub Core Lib -->
<script src="https://pubnub.a.ssl.fastly.net/pubnub-3.1.min.js"></script>

<!-- Import WebSocket Emulation Lib -->
<script src="websocket.js"></script>

<!-- Use WebSocket Constructor for a New Socket Connection -->
<script>(function() {

    "use strict"

    var socket = new WebSocket('wss://pubsub.pubnub.com/demo/demo/my_channel')

    // On Message Receive
    socket.onmessage = function(evt) {
        console.log('socket receive');

    // On Socket Close
    socket.onclose = function() {
        console.log('socket closed');

    // On Error
    socket.onerror = function() {
        console.log('socket error');

    // On Connection Establish
    socket.onopen = function(evt) {
        console.log('socket open');

        // Send a Message!
        socket.send('hello world!');

    // On Send Complete
    socket.onsend = function(evt) {
        console.log('socket send');



To Disable SSL WSS Secure Sockets:

<!-- NON-SSL Import PubNub Core Lib -->
<script src="http://pubnub.a.ssl.fastly.net/pubnub-3.1.min.js"></script>

<!-- NON-SSL Import WebSocket Emulation Lib -->
<script src="websocket.js"></script>

<!-- NON-SSL Use WebSocket Constructor for a New Socket Connection -->
<script>(function() {

    // Note "ws://" rather than "wss://"
    var socket = new WebSocket('ws://pubsub.pubnub.com/demo/demo/my_channel')