Skip to content

Instantly share code, notes, and snippets.

@tanaka-takayoshi
Last active September 23, 2020 04:18
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tanaka-takayoshi/693decf0a175fe4a14529f56233a9419 to your computer and use it in GitHub Desktop.
socket.io + ioredis instrumented with newrelic
version: '3'
services:
redis:
image: "redis:alpine"
ports:
- "6379:6379"
expose:
- 6379
restart:
always
dev:
image: node:12
volumes:
- .:/usr/src/service
working_dir: /usr/src/service
command: bash -c "npm install && npm run dev"
ports:
- 1234:1234
- 1235:1235
- 6060:6060
expose:
- 1234
- 1235
- 6060
- 80
'use strict';
const newrelic = require('newrelic');
const Redis = require('ioredis');
var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);
const redisClient = new Redis({
host: process.env.REDIS_HOST || 'redis',
port: 6379
});
io.on('connection', function (socket) {
socket.on('message', function (data) {
newrelic.startWebTransaction('/websocket/message', async function transactionHandler() {
const channel = 'ioredis_channel';
await redisClient.xadd(channel, '*', 'message', data);
await redisClient.xread('STREAMS', channel, '0');
socket.emit(data);
})
})
})
http.listen(6060, () => {
console.log('listening on *:6060');
});
'use strict';
const newrelic = require('newrelic');
const Redis = require('ioredis');
var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);
const redisClient = new Redis({
host: process.env.REDIS_HOST || 'redis',
port: 6379
});
io.on('connection', function (socket) {
socket.on('message', function (data) {
const channel = 'ioredis_channel';
await redisClient.xadd(channel, '*', 'message', data);
await redisClient.xread('STREAMS', channel, '0');
socket.emit(data);
})
})
http.listen(6060, () => {
console.log('listening on *:6060');
});
{
"name": "newrelic-nodejs-lab",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "node index.js"
},
"author": "Takayoshi Tanaka <ttanaka@newrelic.com>",
"license": "MIT",
"dependencies": {
"newrelic": "6.13.0",
"ioredis": "4.17.3",
"socket.io": "2.3.0",
"express": "4.17.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment