Does Socket Io Involve Disk Io

Does Socket IO involve Disk IO?

Not directly. TCP / UDP network sockets, over localhost, or a UNIX Domain Socket will operate in memory. UNIX Domain Sockets are typically the fastest option outside of dropping into kernel space with a module.

sockets over localhost pipes are nearly as simple as a couple of memcpy's between userspace and kernel space and back. In TCP case, you have the stack overhead.

Both files and sockets share the kernel abstraction of descriptor table, but that doesn't imply an actual file.

Of course, the database may trigger some write to a log, as a result of your transaction.

Performance of Socket write vs disk write

You have few choices:

  • local storage is usually faster than network
  • you could use async logging to disk, so your process fires and forgets (which is fast!)
  • logstash can read from Unix Domain Sockets, if you are on *nix; these are usually faster than I/O

If you are writing somewhere fast and from there it is being forwarded in a slower fashion (logstash logging over network to some Elastic instance) where is the buffering happening? Such setup will generate growing backlog of messages yet to be shipped if the logging happens at high rate for prolonged period of time.



In the above scenarios buffering will happen (respectively):

  • direct sync write to disk: final log file on the disk is the buffer
  • async logging framework: buffers could eat into your heap or process memory (when outside of heap, or in some kernel area, therefore in RAM)
  • unix domain sockets: buffered in the kernel space, so RAM again

In the last 2 options things will get increasingly creaky in constant high volume scenario.


Test and profile...
or just log to the local disk and rotate the files, deleting old ones.

How is the socket.io directory recognised

The socket.io server listens for all incoming requests that start with /socket.io and "steals" those requests for itself, keeping them from the regular http server that the socket.io server is sharing.

When the socket.io server sees that this is a request for the socket.io.js file, the socket.io server then reaches into its own node_modules/socket.io/client-dist directory to get the client-side socket.io.js file and sends it back to the client.

If you look at what you will find in node_modules/socket.io/client-dist directory, you will see the file socket.io.js sitting there. That's the file that the socket.io server sends back to the client. This is client-side code, only on the server for the purposes of being sent to the client when it asks for it.

Keep in mind that responses to incoming paths with a nodejs http server are not necessarily about file directories on the server at all. If any code hooked up to the http server handles an incoming request, it can decide what it wants to send as the response, from anywhere in the server (whether from a file or not). Only specific middleware tools like express.static() look on the server's hard disk for a directory that matches the incoming request.

What role does Socket.IO have with Node.js

node.js is a general purpose javascript-based run-time environment (somewhat similar to other language runtimes like python in scope). You can create apps in it that don't even use the network. It is often used as a web server for created web apps and has a great set of tools and rich library of add-ons for doing so. It does not need socket.io.

socket.io is a specific library to enable web-socket-like communication between a client and a server (e.g. a chat room app is the canonical example). The server side of socket.io assumes a javascript run-time (because it's written in javascript) so that generally means node.js (though I'm not sure if a different JS runtime could perhaps be substituted).

You can think of node.js like the platform and socket.io like a specific tool to do a specific job that runs on that platform. You would use socket.io (on top of node.js) if you needed web socket connectivity between client and server.

You would use only node.js if you need any of the other things node is good at, but did not need websocket connectivity.


websockets themselves can be programmed on the server side without socket.io and without node.js. They could be programmed in strait C++ or in Java. But socket.io (running in node) provides a very easy way to set them up because the socket.io library covers both client and server in one library and one API and it's all in the same language (javascript). Look at the chat room app example on the socket.io site and you will be unlikely to find any other solution that can accomplish that in as few lines of code as it does and with the same interface on client and server.

If you were only setting up a websocket server (no web server or web app of any kind), you could still use node and socket.io and use it just for the websocket server and it would still be quite efficient. While node is capable of doing lots of other things, if you don't configure and install all those other things, they aren't costing you anything - they are just unused capabilities that aren't running.

I should add that one other thing the socket.io library does is it handles an auto-negotiation between client and server to find the best channel for the client and server to communicate on. If websockets are available, then socket.io will likely use them, but if web sockets are not available, socket.io has alternate methods that will work (even in older browsers). That functionality comes for free in socket.io without you even doing anything.


In case this isn't completely clear to you, websockets are typically used to provide real-time communication between client and server. While clients can ask for data from a server at any time with an ajax call or a web page request, what websockets allow is a two way real-time communication between client and server and the biggest advantage of websockets is that a server can send a client real-time data at any time while they are connected.

For example, I have a web page that receives real-time data from my server anytime the web page is open. The web page is served over the typical node.js web server installation, but the real-time data is sent from server to client over a websocket connection.

In addition, if there's a chatty conversation happening between client and server, websockets can be much more efficient than a series of ajax calls because with a websocket, a connection is opened once and used repeatedly whereas with ajax, each successive ajax call is like a new connection.

How many socket.io objects can be held in a JS object in memory before noticeable degradation of performance?

Ten thousand is a good estimate before you start seeing performance "drops," and even then, probably not noticeable until you reach 20k or 50k socket objects. If you consider the size in memory of a socket object versus that of a standard Apache connection (2-3MB per connection last I read) then you really don't have much to be concerned about.

Socket.io - listen events in separate files in node.js

Nope, just use the same "io" object.

File1.js

exports = module.exports = function(io){
io.sockets.on('connection', function (socket) {
socket.on('file1Event', function () {
console.log('file1Event triggered');
});
});
}

File2.js

exports = module.exports = function(io){
io.sockets.on('connection', function (socket) {
socket.on('file2Event', function () {
console.log('file2Event triggered');
});
});
}

app.js

var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
, file1 = require('./File1')(io)
, file2 = require('./File2')(io)

app.listen(3000);

function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}

res.writeHead(200);
res.end(data);
});
}

index.html

<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost');
socket.emit('file1Event'); // 'file1Event triggered' will be shown
socket.emit('file2Event'); // 'file2Event triggered' will be shown
</script>


Related Topics



Leave a reply



Submit