Skip to content

Instantly share code, notes, and snippets.

@PotOfCoffee2Go
Last active November 7, 2025 03:32
Show Gist options
  • Select an option

  • Save PotOfCoffee2Go/b35bb5f0f7e3b0755cecf8f43b51b2f2 to your computer and use it in GitHub Desktop.

Select an option

Save PotOfCoffee2Go/b35bb5f0f7e3b0755cecf8f43b51b2f2 to your computer and use it in GitHub Desktop.
module that implemts a Node REPL using a socket for I/O
/*\
* repl-socket.js
* A module that implemts a Node REPL using a socket for I/O
* Run the server in a terminal window
* Run clients in othr terminal widows
*
* Is available to local network clients by default
*
* gist: https://gist.github.com/PotOfCoffee2Go/b35bb5f0f7e3b0755cecf8f43b51b2f2
* author: https://github.com/PotOfCoffee2Go
* license: MIT
*
* -------------
* Server usage:
* create file server.js
#!/usr/bin/env node
const replserver = require('./repl-socket').server();
*
* chmod +x ./server.js
*
* from terminal prompt './server.js'
*
* -------------
* Client usage:
* create file client.js
#!/usr/bin/env node
const replclient = require('./repl-socket').client();
*
* chmod +x ./client.js
*
* from terminal prompt './client.js'
*
\*/
#!/usr/bin/env node
const replclient = require('./repl-socket').client();
/*\
* repl-socket.js
* A module that implemts a Node REPL using a socket for I/O
* Run the server in a terminal window
* Run clients in othr terminal widows
*
* Is available to local network clients by default
*
* gist: https://gist.github.com/PotOfCoffee2Go/b35bb5f0f7e3b0755cecf8f43b51b2f2
* author: https://github.com/PotOfCoffee2Go
* license: MIT
*
* -------------
* Server usage:
* create file server.js
#!/usr/bin/env node
const replserver = require('./repl-socket').server();
*
* chmod +x ./server.js
*
* from terminal prompt './server.js'
*
* -------------
* Client usage:
* create file client.js
#!/usr/bin/env node
const replclient = require('./repl-socket').client();
*
* chmod +x ./client.js
*
* from terminal prompt './client.js'
*
\*/
"use strict";
const { log } = console;
const repl = require('node:repl');
const net = require('node:net');
exports.server = function replServer(port = 9090, host = '0.0.0.0') {
net.createServer((socket) => {
const $rt = repl.start({
prompt: `repl> `, ignoreUndefined: true,
input: socket, output: socket, terminal: true,
useColors: true, useGlobal: false
});
$rt.on('exit', () => { socket.end(); });
$rt.context.$rt = $rt;
$rt.context.socket = socket;
}).listen(port, host, () => {
log(`REPL server started on port ${port}`);
});
}
exports.client = function replClient(port = 9090) {
// Create a TCP client
process.stdin.setRawMode(true);
const client = net.createConnection({ port }, () => {
log(`Connected to REPL server on port: ${port}`);
});
// Handle connection end
client.on('end', () => {
log('Disconnected from server');
process.exit(0);
});
// Handle errors
client.on('error', (err) => {
console.error('Connection error:', err);
});
// Display results to stdout
client.on("data", (buffer) => {
process.stdout.write(buffer);
});
// if the user types anything, send it to the socket
process.stdin.on("data", (buffer) => {
client.write(buffer);
});
}
#!/usr/bin/env node
const replserver = require('./repl-socket').server();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment