Skip to content

Instantly share code, notes, and snippets.

@dbarria
Last active November 12, 2018 15:34
Show Gist options
  • Save dbarria/c6cc5041ea8dacedacb022aa01dc76b0 to your computer and use it in GitHub Desktop.
Save dbarria/c6cc5041ea8dacedacb022aa01dc76b0 to your computer and use it in GitHub Desktop.
pubnub checker
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Begin of LetsTalk script -->
<script src="https://cdn.pubnub.com/sdk/javascript/pubnub.4.21.5.js"></script>
<!-- End of LetsTalk script -->
</head>
<body>
<h1>Pubnub</h1>
<script>
data = {
//Channel granted forever for authkey '2D672o9K'
channel: 'client-test',
uuid: 'web_client',
authKey: '2D672o9K',
subkey: 'sub-c-1491a222-7c85-11e7-9d24-02ee2ddab7fe',
pubkey: 'pub-c-dc6ad3d8-08e4-42d9-915b-53d39d42d66f'
}
var pubnub = new PubNub({
publishKey : data.pubkey,
subscribeKey : data.subkey,
uuid: data.uuid,
authKey: data.authKey,
ssl: true,
restore: true
});
pubnub.addListener({
status: function(statusEvent) {
if (statusEvent.category === "PNConnectedCategory") {
console.log(statusEvent.operation + ': Client connection to PubNub successful')
publishSampleMessage();
}else{
console.log(statusEvent.operation + ': Client connection to PubNub unsuccessful. Reason ' + statusEvent.category)
}
},
message: function(m) {
if (messageSuccessfulConditions(m)) {
console.log('Client can receive a message')
}
}
});
function messageSuccessfulConditions(m){
return 'description' in m.message && m.message.description == 'hello world!' && m.publisher == data.uuid && m.subscribedChannel == data.channel
}
function publishSampleMessage(){
var publishConfig = {
channel : data.channel,
message: {
title: "greeting",
description: "hello world!"
}
}
pubnub.publish(publishConfig, function(status, response) {
if (status.statusCode == 200){
console.log("Client can publish a message")
}
})
}
pubnub.subscribe({
channels: [data.channel]
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment