-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from appknox/error-cleanup
Add error handling, logging, path change /websocket and package upgrade
- Loading branch information
Showing
6 changed files
with
521 additions
and
294 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }); | ||
* | ||
*/ |
Oops, something went wrong.