Node.js Unix socket not echoing

I am trying to create a file which I can cat or echo strings to and receive strings back as output. If I understand correctly I can do this with a Unix socket.

I’ve tried to write a server but I can’t seem to get it to recognize my input:

'use strict';

var net = require('net'),
    fs = require('fs'),
    socketAddress = '/tmp/test-socket',
    server;

server = net.createServer(function (client) {
    var whole = '';
    client.on('data', function (data) {
        whole += data;
    });
    client.on('end', function () {
        client.write(whole);
    });
});

fs.unlink(socketAddress, function (error) {
    if (error && error.code !== 'ENOENT') {
        throw error;
    }
    server.listen(socketAddress, function () {
        console.log('Socket listening at ' + socketAddress);
    });
});

I try to do echo 'Hello World!' | /tmp/test-socket and I get bash: /tmp/test-socket: No such device or address. But ls -l /tmp yields:

srwxr-xr-x 1 jackson jackson 0 Jun 1 01:10 test-socket

Please advise on how I can get this socket to echo the strings I write to it.


Source: bash

Leave a Reply