Skip to content

Instantly share code, notes, and snippets.

@nhocki
Last active November 4, 2015 00:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nhocki/f2ec831e6203c62d2d54 to your computer and use it in GitHub Desktop.
Save nhocki/f2ec831e6203c62d2d54 to your computer and use it in GitHub Desktop.
PubNub concept
PUBNUB_PUBLISH_KEY=YourPublishKey
PUBNUB_SUBSCRIBE_KEY=YourSubscribeKey
node_modules
.env

Pub Nub client server test

This code aims to have an example on how we can use channel_groups to have one bot connected to a lot of channels with just 1 connection.

Each channel_group can hold up to 2K channels and a server can subscribe to up to 10 channe_groups within the same connection, this means we can listen to 20K channels with just one TCP connection.

Installing

  1. git clone git@gist.github.com:f2ec831e6203c62d2d54.git pubchat
  2. cd pubchat && npm install

Clients

The clients will need to add the channel into a channel group but still publish to the channel (and subscribe to the channel).

We make clients add the channel to the group because they will be the ones getting the channel and group names from the API.

Server

The server will subscribe to a channel group and will get messages from all the channels belonging to that group.

The channel that a message was sent to can be retrieved from the parameters sent to the subscribe function.

Running the test

Make sure you install everything with npm install.

To run the test make sure you have a .env file with everything you need, look at .env.example to know what's needed.

Once you have your env set up, run:

forego start

That will run 1 server and 1 client. If you have multiple clients you can run:

forego start -c all=1,client=60
// client.js
var dotenv = require('dotenv').load();
var pubnub = require("pubnub")({
ssl : true, // <- enable TLS Tunneling over TCP
publish_key : process.env.PUBNUB_PUBLISH_KEY,
subscribe_key : process.env.PUBNUB_SUBSCRIBE_KEY,
auth_key : 'user-' + process.env.PORT,
});
console.log("Using auth key: user-" + process.env.PORT)
pubnub.auth(auth_key='user-' + process.env.PORT)
pubnub.time(function(time){ console.log("Connected at: ", time) } );
// TODO: Fix dyno: web.123
var channel = "ride-" + process.env.PORT
var channel_group = process.env.GROUP || 'ride-group-2'
var sendMessage = function(){
pubnub.publish({
auth_key: 'user-' + process.env.PORT,
channel: channel,
message: 'Hello! from process: ' + process.pid + ' Rand: ' + Math.random(),
callback: function(m){ },
error: function(m){ }
});
}
var seconds = Math.floor(Math.random() * 5) + 1
console.log("Process " + process.pid + " sending every " + seconds)
setInterval(sendMessage, seconds * 1000)
pubnub.subscribe({
auth_key: 'user-' + process.env.PORT,
channel: channel,
message: function() { console.log("5500: ", JSON.stringify(arguments)) },
error: function(m) { console.log("5500 Error: ", JSON.stringify(m)) },
});
{
"name": "pubnubchat",
"version": "1.0.0",
"description": "Testing PubNub",
"main": "client.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"repository": {
"type": "git",
"url": "git@gist.github.com:f2ec831e6203c62d2d54"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://gist.github.com/f2ec831e6203c62d2d54"
},
"homepage": "https://gist.github.com/f2ec831e6203c62d2d54",
"dependencies": {
"async": "^1.5.0",
"dotenv": "^1.2.0",
"pubnub": "^3.7.15"
}
}
server: node server.js
client: node client.js
// server.js
//
// connect to a lot of channels and output what you get
var dotenv = require('dotenv').load();
var async = require('async')
var pubnub = require("pubnub")({
ssl : true, // <- enable TLS Tunneling over TCP
manage : true,
publish_key : process.env.PUBNUB_PUBLISH_KEY,
subscribe_key : process.env.PUBNUB_SUBSCRIBE_KEY,
secret_key : process.env.PUBNUB_SECRET_KEY,
auth_key: "server",
});
var handleMessage = function(msg, meta, group, time, channel) {
console.log("Received message on " + channel + ": ", msg);
}
var handleError = function(error) {
console.log("ERROR!: ", JSON.stringify(error));
}
var channel_group = process.env.GROUP || 'ride-group-2'
pubnub.subscribe({
channel_group: channel_group,
message: handleMessage,
error: handleError
});
pubnub.grant({
channel_group: channel_group,
auth_key: "server",
read: true,
write: true,
manage: true,
ttl: 0,
callback: function() {
console.log('server access granted');
},
error: function(e) {
console.log("Grant error: ", JSON.stringify(e))
},
})
var addChannel = function(group, channel, cback){
return function(){
pubnub.channel_group_add_channel({
channel: channel,
channel_group: group,
callback: cback,
})
}
}
var grantAccess = function(user, channel){
return function(){
pubnub.grant({
channel: channel,
auth_key: user,
read: true,
write: true,
ttl: 0,
callback: function(){
console.log(user + " was granted access to " + channel);
}
})
}
}
var q = async.queue(function(payload, cb){
cb()
}, 2)
var noop = function() {}
for(var i = 0; i <= 10; ++i) {
var number = 5000 + 100 * i
var tmp_channel = 'ride-' + number
var tmp_user = 'user-' + number
var name = tmp_user + " to channel " + tmp_channel
var fn = addChannel(channel_group, tmp_channel, grantAccess(tmp_user, tmp_channel))
q.push({}, fn);
}
pubnub.time(function(time){ console.log("Subscribed at: ", time) } );
console.log("Subscribed!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment