Memory usage
The resources consumed by your Socket.IO server will mainly depend on:
- the number of connected clients
- the number of messages (basic emit, emit with acknowledgement and broadcast) received and sent per second
The memory usage of the Socket.IO server should scale linearly with the number of connected clients.
By default, a reference to the first HTTP request of each session is kept in memory. This reference is needed when working with express-session
for example (see here), but can be discarded to save memory:
io.engine.on("connection", (rawSocket) => {
rawSocket.request = null;
});
The source code to reproduce the results presented in this page can be found there.
See also:
Memory usage per WebSocket server implementation
The memory usage of the Socket.IO server heavily depends on the memory usage of the underlying WebSocket server implementation.
The chart below displays the memory usage of the Socket.IO server, from 0 up to 10 000 connected clients, with:
- a Socket.IO server based on the
ws
package (used by default) - a Socket.IO server based on the
eiows
package, a C++ WebSocket server implementation (see installation steps) - a Socket.IO server based on the
µWebSockets.js
package, a C++ alternative to the Node.js native HTTP server (see installation steps) - a plain WebSocket server based on the
ws
package
Tested on Ubuntu 22.04 LTS
with Node.js v20.3.0
, with the following package versions:
socket.io@4.7.2
eiows@6.7.2
uWebSockets.js@20.33.0
ws@8.11.0
Memory usage over time
The chart below displays the memory usage of the Socket.IO server over time, from 0 up to 10 000 connected clients.
For demonstration purposes, we manually call the garbage collector at the end of each wave of clients:
io.on("connection", (socket) => {
socket.on("disconnect", () => {
const lastToDisconnect = io.of("/").sockets.size === 0;
if (lastToDisconnect) {
gc();
}
});
});
Which explains the clean drop in memory usage when the last client disconnects. This is not needed in your application, the garbage collection will be automatically triggered when necessary.