37

How to create a named pipe in node.js?

P.S.: For now I'm creating a named pipe as follows. But I think this is not best way

var mkfifoProcess = spawn('mkfifo',  [fifoFilePath]);
mkfifoProcess.on('exit', function (code) {
    if (code == 0) {
        console.log('fifo created: ' + fifoFilePath);
    } else {
        console.log('fail to create fifo with code:  ' + code);
    }
});
wako
  • 785
  • 2
  • 7
  • 9

3 Answers3

43

Working with named pipes on Windows

Node v0.12.4

var net = require('net');

var PIPE_NAME = "mypipe";
var PIPE_PATH = "\\\\.\\pipe\\" + PIPE_NAME;

var L = console.log;

var server = net.createServer(function(stream) {
    L('Server: on connection')

    stream.on('data', function(c) {
        L('Server: on data:', c.toString());
    });

    stream.on('end', function() {
        L('Server: on end')
        server.close();
    });

    stream.write('Take it easy!');
});

server.on('close',function(){
    L('Server: on close');
})

server.listen(PIPE_PATH,function(){
    L('Server: on listening');
})

// == Client part == //
var client = net.connect(PIPE_PATH, function() {
    L('Client: on connection');
})

client.on('data', function(data) {
    L('Client: on data:', data.toString());
    client.end('Thanks!');
});

client.on('end', function() {
    L('Client: on end');
})

Output:

Server: on listening
Client: on connection
Server: on connection
Client: on data: Take it easy!
Server: on data: Thanks!
Client: on end
Server: on end
Server: on close

Note about pipe names:

C/C++ / Nodejs:
\\.\pipe\PIPENAME CreateNamedPipe

.Net / Powershell:
\\.\PIPENAME NamedPipeClientStream / NamedPipeServerStream

Both will use file handle:
\Device\NamedPipe\PIPENAME

befzz
  • 1,232
  • 13
  • 11
  • Any way to send a HTTP request over that pipe? And can other clients send messages to other clients? – oligofren Oct 13 '15 at 20:29
  • Are pipes basically the same as unix sockets, but work under windows? – NiCk Newman Jun 18 '16 at 20:44
  • 3
    @NiCkNewman: Windows (NT) Named Pipes are highly analogous to Unix domain sockets, yes. They have some differences (separate filesystem namespace, use file APIs for IO and their own APIs for management rather than using socket APIs, don't persist when server process goes away, passing a HANDLE is different from passing an fd, Windows pipes *can* be accessed over the network if you enable it), but the use cases are near-identical. Securing Windows pipes can be tricky but is possible. Unix (named) pipes / FIFOs are much more limited (one-way only, can't support multiple clients, etc.) – CBHacking Apr 06 '17 at 23:24
34

Looks like name pipes aren't and won't be supported in Node core - from Ben Noordhuis 10/11/11:

Windows has a concept of named pipes but since you mention mkfifo I assume you mean UNIX FIFOs.

We don't support them and probably never will (FIFOs in non-blocking mode have the potential to deadlock the event loop) but you can use UNIX sockets if you need similar functionality.

https://groups.google.com/d/msg/nodejs/9TvDwCWaB5c/udQPigFvmgAJ

Named pipes and sockets are very similar however, the net module implements local sockets by specifying a path as opposed to a host and port:

Example:

var net = require('net');

var server = net.createServer(function(stream) {
  stream.on('data', function(c) {
    console.log('data:', c.toString());
  });
  stream.on('end', function() {
    server.close();
  });
});

server.listen('/tmp/test.sock');

var stream = net.connect('/tmp/test.sock');
stream.write('hello');
stream.end();
jpillora
  • 5,194
  • 2
  • 44
  • 56
-1

Maybe use fs.watchFile instead of named pipe ? See documentation

Vodun
  • 1,377
  • 1
  • 10
  • 12
  • 1
    Unfortunately in my use case I need exactly named pipe and I can't use watchFile method :-( – wako Aug 02 '12 at 12:44