Skip to content

Instantly share code, notes, and snippets.

@ianjennings
Last active August 4, 2017 17:10
Show Gist options
  • Save ianjennings/5f6c7353ccc7fdee6aaa9830c526f1d4 to your computer and use it in GitHub Desktop.
Save ianjennings/5f6c7353ccc7fdee6aaa9830c526f1d4 to your computer and use it in GitHub Desktop.
// runs on setup
let request = require('request');
let PubNub = require('pubnub');
let channel = 'chan' + new Date().getTime();
let myUUID = 'uuid' + new Date().getTime();
let myAuthKey = "auth" + new Date().getTime();
request.post({
url: 'https://pubsub.pubnub.com/v1/blocks/sub-key/sub-c-67db0e7a-50be-11e7-bf50-02ee2ddab7fe/auther',
json: {
authKey: myAuthKey,
channel: channel,
uuid: myUUID
}
}, (err, httpResponse, body) => {
console.log('pfunct response', body)
let pn = new PubNub({
authKey: myAuthKey,
publishKey: 'asdfasfd',
subscribeKey: 'asdfasfdasdf'
});
pn.addListener({
message: (message) => {
console.log('incoming', message)
}
});
pn.subscribe({
channels: [channel]
});
console.log('publishing')
pn.publish({
channel: channel,
message: 'test'
}, function(a,b,c){
console.log(a,b,c)
})
});
export default (request, response) => {
const pubnub = require('pubnub');
let bodyString = request.body;
response.status = 200;
return request.json().then((body) => {
let chan = body.channel;
let myAuthKey = body.authKey;
let myUUID = body.uuid;
console.log(chan, myAuthKey, myUUID)
return pubnub.grant({
channels: [chan],
read: true, // false to disallow
write: true, // false to disallow,
authKeys: [myAuthKey],
ttl: 0
}).then((res) => {
return response.send(body);
}).catch((error) => {
// handle error
console.log('couldnt grant')
console.log(error)
return response.send(body);
});
}).catch((err) => {
// console.log(err)
return response.send("Malformed JSON body.");
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment