Guest User

PubNub SDK-JS v4

a guest
Sep 22nd, 2017
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3.    <head>
  4.       <meta charset="utf-8">
  5.       <title>PubNub Tester</title>
  6.       <script src="https://cdn.pubnub.com/sdk/javascript/pubnub.4.15.1.js"></script>
  7.    </head>
  8.    <body>
  9.       <script type="text/javascript">
  10.          // Initialize for Read Only Client
  11.            console.log('init PubNub.');    
  12.            pubnub = new PubNub({
  13.              publishKey: 'demo',
  14.              subscribeKey: 'demo',
  15.              uuid: 'myDemo'
  16.            })
  17.          
  18.            console.log("addListener..");
  19.            pubnub.addListener({
  20.              status: function(statusEvent) {
  21.                if (statusEvent.category === "PNConnectedCategory") {
  22.                  console.log("PNConnectedCategory..");
  23.                  publishSampleMessage();
  24.                }
  25.              },
  26.              message: function(message) {
  27.                console.log("New Message!!", message.message);          
  28.              },
  29.              presence: function(presenceEvent) {
  30.                // handle presence
  31.              }
  32.            })
  33.          
  34.            console.log("Subscribing..");
  35.            pubnub.subscribe({
  36.              channels: ['myDemo']
  37.            });
  38.          
  39.            function publishSampleMessage() {
  40.              console.log("Since we're publishing on subscribe connectEvent, we're sure we'll receive the following publish.");
  41.              var publishConfig = {
  42.                channel: "myDemo",
  43.                message: "I'm here, I'm alive!!"
  44.              }
  45.              pubnub.publish(publishConfig, function(status, response) {
  46.                console.log(status, response);
  47.              })
  48.            }
  49.          
  50.          function onClick() {
  51.            publishSampleMessage();
  52.          }
  53.          
  54.       </script>
  55.       <button onclick="onClick()">start</button>
  56.    </body>
  57. </html>
Add Comment
Please, Sign In to add comment