Skip to content

Commit

Permalink
Merge pull request #2 from peteriman/feat/socket.io-namespace
Browse files Browse the repository at this point in the history
feat: socket.io namespace support
  • Loading branch information
lamweili authored Jun 9, 2022
2 parents 33f93f2 + 8c7b77f commit 64f1b52
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 17 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ title: 'Express Status', // Default title
theme: 'default.css', // Default styles
path: '/status',
socketPath: '/socket.io', // In case you use a custom path
websocket: existingSocketIoInstance,
namespace: '/', // socket.io namespace
port: null, // socket.io port
websocket: null, // In case you use an existing socket.io instance
spans: [{
interval: 1, // Every second
retention: 60 // Keep 60 datapoints in memory
Expand All @@ -59,15 +61,14 @@ chartVisibility: {
cpu: true,
mem: true,
load: true,
eventLoop: true,
heap: true,
eventLoop: true,
responseTime: true,
rps: true,
statusCodes: true
},
healthChecks: [],
ignoreStartsWith: '/admin'

ignoreStartsWith: '/admin',
healthChecks: []
```

## Health Checks
Expand Down
5 changes: 3 additions & 2 deletions src/helpers/default-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ module.exports = {
theme: 'default.css',
path: '/status',
socketPath: '/socket.io',
namespace: '/',
port: null,
websocket: null,
spans: [
{
interval: 1,
Expand All @@ -17,8 +20,6 @@ module.exports = {
retention: 60,
},
],
port: null,
websocket: null,
iframe: false,
chartVisibility: {
cpu: true,
Expand Down
5 changes: 4 additions & 1 deletion src/helpers/socket-io-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ module.exports = (server, config) => {
if (config.websocket !== null) {
io = config.websocket;
} else {
io = socketIo(server);
io = socketIo(server, { path: config.socketPath });
}
if (typeof io.of === 'function') {
io = io.of(config.namespace);
}

io.on('connection', socket => {
Expand Down
40 changes: 34 additions & 6 deletions src/helpers/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,35 @@ module.exports = config => {
return defaultConfig.chartVisibility;
};

const prependWithSlash = string => {
if (string && !string.startsWith('/')) {
return `/${string}`;
}
return string;
};

config.title =
typeof config.title === 'string' ? config.title : defaultConfig.title;
config.theme =
typeof config.theme === 'string' ? config.theme : defaultConfig.theme;
config.path =
typeof config.path === 'string' ? config.path : defaultConfig.path;
prependWithSlash(
typeof config.path === 'string'
? config.path
: defaultConfig.path
);
config.namespace =
prependWithSlash(
typeof config.namespace === 'string'
? config.namespace
: defaultConfig.namespace
);
config.socketPath =
typeof config.socketPath === 'string' ? config.socketPath : defaultConfig.socketPath;
prependWithSlash(
typeof config.socketPath === 'string'
? config.socketPath
: defaultConfig.socketPath
);
config.spans =
typeof config.spans === 'object' ? config.spans : defaultConfig.spans;
config.port =
Expand All @@ -37,14 +58,21 @@ module.exports = config => {
? mungeChartVisibility(config.chartVisibility)
: defaultConfig.chartVisibility;
config.ignoreStartsWith =
typeof config.path === 'string'
? config.ignoreStartsWith
: defaultConfig.ignoreStartsWith;
prependWithSlash(
typeof config.ignoreStartsWith === 'string'
? config.ignoreStartsWith
: defaultConfig.ignoreStartsWith
);

config.healthChecks =
Array.isArray(config.healthChecks)
? config.healthChecks
: defaultConfig.healthChecks
: defaultConfig.healthChecks;
config.healthChecks.forEach(healthCheck => {
if (healthCheck.path) {
healthCheck.path = prependWithSlash(healthCheck.path);
}
});

return config;
};
1 change: 1 addition & 0 deletions src/middleware-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const middlewareWrapper = config => {
title: validatedConfig.title,
port: validatedConfig.port,
socketPath: validatedConfig.socketPath,
namespace: validatedConfig.namespace,
bodyClasses,
script: fs.readFileSync(path.join(__dirname, '/public/javascripts/app.js')),
style: fs.readFileSync(path.join(__dirname, '/public/stylesheets/', validatedConfig.theme))
Expand Down
1 change: 1 addition & 0 deletions src/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ <h1>{{status}}</h1>
<script>
var port = '{{port}}';
var socketPath = '{{socketPath}}';
var namespace = '{{namespace}}';
{{{script}}}
</script>
</body>
Expand Down
6 changes: 3 additions & 3 deletions src/public/javascripts/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
eslint-disable no-plusplus, no-var, strict, vars-on-top, prefer-template,
func-names, prefer-arrow-callback, no-loop-func
*/
/* global Chart, location, document, port, socketPath, parseInt, io */
/* global Chart, location, document, port, socketPath, namespace, parseInt, io */

'use strict';

Expand All @@ -13,9 +13,9 @@ Chart.defaults.global.elements.line.backgroundColor = 'rgba(0,0,0,0)';
Chart.defaults.global.elements.line.borderColor = 'rgba(0,0,0,0.9)';
Chart.defaults.global.elements.line.borderWidth = 2;

var socket = io(location.protocol + '//' + location.hostname + ':' + (port || location.port), {
var socket = io(location.protocol + '//' + location.hostname + ':' + (port || location.port) + namespace, {
path: socketPath,
transports: ["websocket"]
transports: ['websocket']
});
var defaultSpan = 0;
var spans = [];
Expand Down

0 comments on commit 64f1b52

Please sign in to comment.