I have a requirement for 9M devices to be communicated to the server, and send messages in every 6seconds, and the amount of data sent from client will be 1KB, and Now I was creating a POC for sockets, and found this library called socket.io.
And My server looks like this:
const { Server } = require('socket.io');
const { hashObjectWithString } = require('./utils');
// Create a basic HTTP server
const server = http.createServer();
const io = new Server(server);
// Socket.IO connection handling
io.on('connection', socket => {
const clientID = socket.handshake.query?.clientID ?? 'unknown';
// Listen for 'channel-receive' events
socket.on('channel-from-client', (msg, ackCB) => {
if (ackCB) {
ackCB('Message Received!');
}
});
// Listen for disconnection
socket.on('disconnect', () => {
console.log('Disconnected' + clientID)
});
socket.conn.on("close", (reason) => {
console.log('Disconnected' + clientID)
});
});
const PORT = process.env.PORT || 8085;
server.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
And Client Looks Like this:
const io = require('socket.io-client');
const heartbeatJSON = require('../heartbeat.json');
for (let id of Array.from(new Array(100).keys())) {
const clientID = id;
const socket = io('http://50.19.44.187:8085', {
query: {
clientID
}
});
// Function to send message to server
const sendMessage = (message) => {
socket.emit('channel-from-client', message, (ack) => {
console.log('ACK', ack);
});
};
// Listen for 'chat message' events from server
socket.on('channel-from-server', (msg, ackCB) => {
console.log('Received message:', msg);
if (ackCB) {
ackCB('Message Received! by: ' + clientID);
}
});
socket.on("connect", () => {
console.log({ msg: " connected ", clientID });
});
socket.on("disconnect", () => {
console.log("Connection disconnected!");
})
// Start by sending a message
const time = 6 * 1000;
setInterval(() => {
sendMessage(heartbeatJSON);
}, time);
}
I use docker for running the server in a Aws ec2-t2 micro server (1v cpu, and 1gb mem). and number of users are 100
I use docker stats
to get the statistics and My test results looks like this for 6s frequency communication.
CPU: 28.02% // highest cpu utilization in the 5mins duration
MEM: 18.43MiB / 952.7MiB
NET: 2.75MB / 599kB
Could someone please explain why this is using more than 25% of the CPU and occasionally less than 10%? I’m at a loss as to why these swings occur; even if I increase the frequency of heartbeat, the CPU consumption started with a higher values above 25%.
Priyajit Nayak is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.