Logs milliseconds for each leg of a websocket event.
Last active
November 8, 2022 17:54
-
-
Save MattSandy/7a5ca0314b343407d30a6fd9affe7cfd to your computer and use it in GitHub Desktop.
SocketIO Client and Server Performance
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
const io = require("socket.io-client"); | |
const socket = io.connect('http://127.0.0.1:3000', { transports: ['websocket'] }); | |
let startTime; | |
const now = () => { | |
const hrTime = process.hrtime(); | |
return hrTime[0] * 1000 + hrTime[1] / 1000000; | |
}; | |
setInterval(function () { | |
startTime = now(); | |
socket.emit('ping',now()); | |
}, 2000); | |
socket.on('pong', function (time) { | |
let t = now(); | |
console.log({ | |
'ping': t - startTime, | |
'pong': t - time | |
}); | |
}); |
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
const express = require('express'); | |
const app = express(); | |
const http = require('http'); | |
const server = http.createServer(app); | |
const { Server } = require("socket.io"); | |
const io = new Server(server); | |
const now = () => { | |
const hrTime = process.hrtime(); | |
return hrTime[0] * 1000 + hrTime[1] / 1000000; | |
}; | |
app.get('/', (req, res) => { | |
res.sendFile(__dirname + '/index.html'); | |
}); | |
io.sockets.on('connection', function (socket) { | |
socket.on('ping', function (time) { | |
console.log(time); | |
socket.emit('pong', now()); | |
console.log({ | |
'ping': now() - time, | |
}) | |
}); | |
}); | |
server.listen(3000, () => { | |
console.log('listening on *:3000'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment