Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Node.js socket server and client
const net = require('net');
const PORT = 6000;
const server = net.createServer((connection) => {
console.log('created server');
connection.on('data', (data) => {
connection.write('pong\n');
});
connection.pipe(connection);
});
server.listen(PORT);
const client = net.createConnection({ port: PORT }, () => {
console.log('created client');
setInterval(() => {
client.write('ping');
}, 1000);
});
client.on('data', (data) => {
console.log('client got data', data.toString());
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.