Skip to content

Instantly share code, notes, and snippets.

Created March 26, 2012 20:09
Show Gist options
  • Save TooTallNate/2209310 to your computer and use it in GitHub Desktop.
Save TooTallNate/2209310 to your computer and use it in GitHub Desktop.
Running a "full-featured" REPL using a net.Server and net.Socket
var net = require('net')
var sock = net.connect(1337)
sock.on('connect', function () {
sock.on('close', function done () {
sock.removeListener('close', done)
process.stdin.on('end', function () {
process.stdin.on('data', function (b) {
if (b.length === 1 && b[0] === 4) {
var repl = require('repl')
var net = require('net')
net.createServer(function (socket) {
var r = repl.start({
prompt: 'socket '+socket.remoteAddress+':'+socket.remotePort+'> '
, input: socket
, output: socket
, terminal: true
, useGlobal: false
r.on('exit', function () {
r.context.socket = socket
Copy link

Hi Nate,
I'm trying to set up a repl with stdout piped to it, this seems like a good demo. But when I run this with node 0.6.19, I get this error:

TypeError: Object #<ReadStream> has no method 'setRawMode'
    at Socket.<anonymous> (nodejs/repl-demo/repl-client.js:10:17)
    at Socket.emit (events.js:64:17)
    at Object.afterConnect [as oncomplete] (net.js:652:10)

What am I missing?

Copy link

@newleafdigital You need node v0.8.0 for this example.

Copy link


Copy link

Is there any way in 0.6 to pipe stdout / console.log to a repl, so while I'm running commands, I can see the logs? It seems like it should be straightforward, but I can't get it to work.

Copy link

    r.context.inspect = function(value) {
        r.outputStream.write('\n' + r.writer(value) + '\n');

This method is useful as callback to inspect values when console.log() is unavailable.
Example of usage:

socket> setTimeout(function() {inspect(user);}, 1000);null;
null // immediate reaction
{ coins: 10 } // after the timeout

Though please be careful: this code crashes the server if repl client disconnects before the timeout.

Copy link

qzaidi commented Apr 21, 2013

Looks like this stopped working with 0.10.0, running the client results in the following error.

throw new Error('Cannot switch to old mode now.');

Copy link

I'm curious, too -- I'd like a remote REPL participant to be able to see the results of whatever they do, in their own REPL, instead of having that output "leak" back into the terminal where the program is running.

Copy link

ghost commented Dec 3, 2013


var socket = require('');

i am not getting remote addeess and it gives undefined


req.headers['x-forwarded-for'] it gives local ip address ...

i want remote ip address

Copy link

how to start the client and server in this application?

Copy link

i tried node repl-server.js it is giving me an error
throw err;

Error: Cannot find module 'C:\D Drive\Learning\repl-server.js'
at Function.Module._resolveFilename (module.js:536:15)
at Function.Module._load (module.js:466:25)
at Function.Module.runMain (module.js:676:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment