Last active
September 23, 2020 04:18
-
-
Save tanaka-takayoshi/693decf0a175fe4a14529f56233a9419 to your computer and use it in GitHub Desktop.
socket.io + ioredis instrumented with newrelic
This file contains hidden or 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
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 |
This file contains hidden or 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
'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'); | |
}); |
This file contains hidden or 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
'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'); | |
}); | |
This file contains hidden or 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
{ | |
"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 <[email protected]>", | |
"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