nx.js
Concepts

Networking

Client and Server TCP connections

nx.js provides a simple API for creating TCP client connections and servers. The TCP socket interface is based on the WinterCG Socket API proposal.

TCP Client

// "tcpbin.com" is a public TCP echo server
const socket = Switch.connect('tcpbin.com:4242');
 
const decoder = new TextDecoder();
const encoder = new TextEncoder();
const reader = socket.readable.getReader();
const writer = socket.writable.getWriter();
 
const payload = 'Hello World!\n';
console.log('Sending:  %j', payload);
await writer.write(encoder.encode(payload));
 
console.log('Received: %j', decoder.decode(await reader.read()));
// Prints "Hello World!\n"
 
await writer.close();
reader.releaseLock();
socket.close();

TCP Server

// A simple echo server
const port = 2323;
 
const server = Switch.listen({
    port,
    async accept(socket) {
        console.log('Client connection established');
        const reader = socket.readable.getReader();
        const writer = socket.writable.getWriter();
        for await (const chunk of reader) {
            if (chunk.done) break;
            await writer.write(chunk.value);
        }
        await writer.close();
        reader.releaseLock();
        console.log('Client closed connection');
    },
});
 
const { ip } = Switch.networkInfo();
console.log('Echo server listening on "%s:%d"', ip, port);

Learn more

On this page