Skip to content

Commit

Permalink
feat: complete flow
Browse files Browse the repository at this point in the history
  • Loading branch information
roylee0704 committed May 14, 2020
1 parent aeb82b3 commit fa9c913
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 6 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ start_pubsub:

stop_pubsub:
docker-compose -f ./centrifugo/docker-compose.yaml down

publisher:
(for i in `seq 1 10000`; do sleep 1; echo New Stock Price; done) | API_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJiYXIiLCJpYXQiOjE1ODk0NDI3ODh9.03oCkrDVT_HGMsEaIsu1q2JsSRGq5rayk-WbXhRcvtY CHANNEL_ID=chat node publisher

subscriber:
TOKEN=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJiYXIiLCJpYXQiOjE1ODk0NTI3Mjd9.jEpIsjLwFfRU_dkjtH6m-4MsUUqXCnLKpqJu1cvILa0 CHANNEL_ID=chat node subscriber
11 changes: 10 additions & 1 deletion centrifugo/.data/config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
{
"admin": true,
"admin_password": "password",
"admin_secret": "secret",
"debug": true,
"anonymous": true,
"v3_use_offset": true,
"token_hmac_secret_key": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJiYXIiLCJpYXQiOjE1ODk0NDI3Mjh9.fN_KOKH2yOc1Fp2QcMaYd2z8-LxVTg7My1kSBLMUrPs",
"prometheus": true,
"graphite": true,
"graphite_host": "localhost",
"graphite_port": 2003,
"token_hmac_secret_key": "shhhhh",
"api_key": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJiYXIiLCJpYXQiOjE1ODk0NDI3ODh9.03oCkrDVT_HGMsEaIsu1q2JsSRGq5rayk-WbXhRcvtY"
}
9 changes: 6 additions & 3 deletions gen-hmac-token.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import jwt from 'jsonwebtoken';

const secretKey = 'shhhhh';
const token = jwt.sign({ foo: 'bar', subject: '' }, 'shhhhh');
console.log('secretKey', secretKey);
console.log('token', token);


const token = jwt.sign({ foo: 'bar' }, 'shhhhh');
console.log(token);
const decoded = jwt.verify(token, secretKey);
console.log('decoded', decoded);
133 changes: 133 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
"author": "roylee0704 <roylee0704@gmail.com> (https://twitter.com/roylee0704)",
"license": "ISC",
"dependencies": {
"axios": "^0.19.2",
"centrifuge": "^2.6.0",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1"
"jsonwebtoken": "^8.5.1",
"readline": "^1.3.0",
"ws": "^7.3.0"
}
}
}
31 changes: 31 additions & 0 deletions publisher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import readline from 'readline';
import axios from 'axios';

const terminal = readline.createInterface({
input: process.stdin,
});


const API_KEY = process.env.API_KEY;
const CHANNEL_ID = process.env.CHANNEL_ID;
const NAME = process.env.NAME;
terminal.on('line', text => {
const message = { name: NAME, text };

const payload = {
"method": "publish",
"params": {
"channel": CHANNEL_ID,
"data": message
}
};

axios.post('http://localhost:8000/api', payload, {
headers: { Authorization: `apikey ${API_KEY}` }
}).then(function (response) {
console.log(`${JSON.stringify(message)} > ${response.status}: ${response.statusText}`);
}).catch(function (error) {
console.log(error);
});

});
Empty file removed server.js
Empty file.
18 changes: 18 additions & 0 deletions subscriber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Centrifuge from 'centrifuge';
import WebSocket from 'ws';

const centrifuge = new Centrifuge('ws://localhost:8000/connection/websocket', {
websocket: WebSocket
});

const TOKEN = process.env.TOKEN;
centrifuge.setToken(TOKEN);

const CHANNEL_ID = process.env.CHANNEL_ID;
centrifuge.subscribe(CHANNEL_ID, function (message) {
console.log(message);
});



console.log(centrifuge.connect());

0 comments on commit fa9c913

Please sign in to comment.