Skip to content

Commit

Permalink
Merge pull request #2 from appknox/error-cleanup
Browse files Browse the repository at this point in the history
Add error handling, logging, path change /websocket and package upgrade
  • Loading branch information
cosmosgenius authored Feb 6, 2020
2 parents d8fad99 + b2dd746 commit 4fc5264
Show file tree
Hide file tree
Showing 6 changed files with 521 additions and 294 deletions.
1 change: 0 additions & 1 deletion .buildpacks

This file was deleted.

20 changes: 20 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# editorconfig.org

root = true


[*]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space
indent_size = 2

[*.hbs]
insert_final_newline = false

[*.{diff,md}]
trim_trailing_whitespace = false
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:9.4.0-alpine
FROM node:13.7.0-alpine

WORKDIR /code

Expand Down
80 changes: 46 additions & 34 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,78 @@
/*
* app.js
* Copyright (C) 2014 dhilipsiva <dhilipsiva@gmail.com>
* Copyright (C) 2020 Appknox <engineering@appknox.com>
*
* Distributed under terms of the MIT license.
*/

var port = process.env.PORT || 8008
, server = require('http').createServer()
, io = require('socket.io')(server)
// , redis = require('redis')
, socketIORedis = require('socket.io-redis')
, redisPort = process.env.REDIS_PORT || 6379
, redisHost = process.env.REDIS_HOST || 'pubsub-redis.pubsub'
, redis = require('redis').createClient
, pub = redis(redisPort, redisHost, { auth_pass: process.env.REDIS_PASSWORD })
, sub = redis(redisPort, redisHost, { auth_pass: process.env.REDIS_PASSWORD })
, adapter = socketIORedis({ pubClient: pub, subClient: sub })
const port = process.env.PORT || 8008;
const server = require('http').createServer();
const io = require('socket.io')(server, {
path: '/websocket',
serveClient: false
});
const socketIORedis = require('socket.io-redis');
const redisPort = process.env.REDIS_PORT || 6379;
const redisHost = process.env.REDIS_HOST || 'localhost';
const redisPassword = process.env.REDIS_PASSWORD;
const redis = require('redis').createClient;
const pub = redis(redisPort, redisHost, { auth_pass: redisPassword });
const sub = redis(redisPort, redisHost, { auth_pass: redisPassword });
const adapter = socketIORedis({ pubClient: pub, subClient: sub });
const pino = require('pino');
const log = pino({
prettyPrint: true,
level: process.env.LOGLEVEL || 'info'
});

io.adapter(adapter);

io.on('connection', function (socket) {

log.debug('Socket connection: ', socket.id);
socket.on('subscribe', function(data) {
socket.join(data.room);
console.log("User joined the room: ", data);
log.debug("User joined the room: ", data);
})

socket.on('unsubscribe', function(data) {
socket.leave(data.room);
console.log("User left the room: ", data);
log.debug("User left the room: ", data);
})

socket.on('disconnect', function(data) {
socket.leave(data.room);
console.log("User quit the room: ", data);
if(data == "ping timeout") {
log.debug("Ping timeout");
return;
}
log.debug("User quit the room: ", data);
})

});

adapter.subClient.on("message", function (channel, message) {

console.log("New Message in Channel: %s", channel);
log.debug(`New Message in Channel: ${channel}`);
log.debug(`Message: ${message}`);
if (channel == "notify") {
data = JSON.parse(message);
data.rooms.forEach(function(room){
io.in(room).emit(data.event, data.data);
});

console.log("new message: ", message);
try {
data = JSON.parse(message);
} catch(error) {
log.error(error, channel, message);
return;
}
if(data && data.rooms && data.rooms.length) {
data.rooms.forEach(function(room){
io.in(room).emit(data.event, data.data);
});
} else {
log.error(`Invalid message: ${message} from channel: ${channel}`);
}
} else {
log.error(`Invalid channel: ${channel} with message: ${message}`);
}

});


adapter.subClient.subscribe("notify");

console.log('server listens on port ' + port);
log.info('server listens on port ' + port);
server.listen(port);

/*
* Usage on client side
*
* socket.emit("subscribe", { room: "user uuid" });
*
*/
Loading

0 comments on commit 4fc5264

Please sign in to comment.