Skip to content
Create a gist now

Instantly share code, notes, and snippets.

Running a "full-featured" REPL using a net.Server and net.Socket
var net = require('net')
var sock = net.connect(1337)
process.stdin.pipe(sock)
sock.pipe(process.stdout)
sock.on('connect', function () {
process.stdin.resume();
process.stdin.setRawMode(true)
})
sock.on('close', function done () {
process.stdin.setRawMode(false)
process.stdin.pause()
sock.removeListener('close', done)
})
process.stdin.on('end', function () {
sock.destroy()
console.log()
})
process.stdin.on('data', function (b) {
if (b.length === 1 && b[0] === 4) {
process.stdin.emit('end')
}
})
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 () {
socket.end()
})
r.context.socket = socket
}).listen(1337)
@benbuckman

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:

repl-client.js:10
  process.stdin.setRawMode(true)
                ^
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?
Thanks!

@TooTallNate
Owner

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

@benbuckman

Thanks

@benbuckman

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.

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

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

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

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

@qzaidi

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

_stream_readable.js:683
throw new Error('Cannot switch to old mode now.');

@sandover

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.

@jigneshnavsoft

var socket = require('engine.io-client');
console.log(socket.remoteAddress);

i am not getting remote addeess and it gives undefined

@jigneshnavsoft

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

i want remote ip address

@pradeeprath22

Using Guile Interactively

When you start up Guile by typing just guile, without a -c argument or the name of a script to execute, you get an interactive interpreter where you can enter Scheme expressions, and Guile will evaluate them and print the results for you. Here are some simple examples.

scheme@(guile-user)> (+ 3 4 5)
$1 = 12
scheme@(guile-user)> (display "Hello world!\n")
Hello world!
scheme@(guile-user)> (values 'a 'b)
$2 = a
$3 = b

This mode of use is called a REPL, which is short for “Read-Eval-Print Loop”, because the Guile interpreter first reads the expression that you have typed, then evaluates it, and then prints the result. http://goatripsindia.com/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.