LogoPear Docs

bare-net

TCP and IPC servers and clients for JavaScript

stable

bare-net — TCP and IPC servers and clients for JavaScript.

npm i bare-net

Usage

const net = require('bare-net')

const server = net.createServer()
server.on('connection', (socket) => socket.on('data', console.log))
server.listen(() => console.log('server is up'))

const { port } = server.address()
const socket = net.createConnection(port)
socket.write('hello world')

API

NetSocket

new NetSocket(opts?: NetOptions)

Source

Create a NetSocket.

Parameters

ParameterTypeDefaultDescription
opts?NetOptionsOptions; readBufferSize defaults to 65536, and allowHalfOpen and eagerOpen to false.

connect(path: string, opts?: PipeConnectOptions, onconnect?: () => void): this

Source

Connect the socket to a path over IPC, or to a port/host over TCP.

Overloads:

connect(path: string, opts?: PipeConnectOptions, onconnect?: () => void): this
connect(path: string, onconnect: () => void): this
connect(port: number, host?: string, opts?: TCPSocketConnectOptions, onconnect?: () => void): this
connect(port: number, host: string, onconnect: () => void): this
connect(port: number, onconnect: () => void): this
connect(opts: NetSocketConnectOptions, onconnect?: () => void): this

Parameters

ParameterTypeDefaultDescription
pathstringThe path to connect to over IPC.
opts?PipeConnectOptionsConnection options passed to the underlying socket; path, port, and host may be given here instead of as positional arguments.
onconnect?() => voidCalled once when the socket emits 'connect'.

connecting: boolean

Source

true while the underlying socket is in the process of connecting.

localAddress: string

Source

Local IP address the socket is connected on, if connected over TCP.

localFamily: string

Source

Local IP address family, if connected over TCP.

localPort: number

Source

Local port the socket is connected on, if connected over TCP.

pending: boolean

Source

true if the socket hasn't been assigned an underlying TCP or IPC socket yet.

readyState: 'open' | 'readOnly' | 'writeOnly' | 'opening'

Source

Current connection state of the socket.

NetSocket.ref(): this

Source

Reference the socket, keeping the event loop alive while it is open.

remoteAddress: string

Source

Remote IP address the socket is connected to, if connected over TCP.

remoteFamily: string

Source

Remote IP address family, if connected over TCP.

remotePort: number

Source

Remote port the socket is connected to, if connected over TCP.

setKeepAlive(enable?: boolean, delay?: number): this

Source

Enable or disable TCP keep-alive on the underlying socket.

Overloads:

setKeepAlive(enable?: boolean, delay?: number): this
setKeepAlive(delay: number): this

Parameters

ParameterTypeDefaultDescription
enable?booleanWhether to enable keep-alive.
delay?numberThe initial delay in milliseconds before the first keep-alive probe is sent.

setNoDelay(enable?: boolean): this

Source

Enable or disable Nagle's algorithm on the underlying socket.

Parameters

ParameterTypeDefaultDescription
enable?booleanWhen true (the default), data is sent immediately without buffering.

setTimeout(ms: number, ontimeout?: () => void): this

Source

Set the socket's inactivity timeout.

Parameters

ParameterTypeDefaultDescription
msnumberThe inactivity timeout in milliseconds; pass 0 to disable the timeout.
ontimeout?() => voidCalled once when the socket emits 'timeout'.

timeout: number

Source

The socket's current inactivity timeout, in milliseconds.

NetSocket.unref(): this

Source

Unreference the socket, allowing the event loop to exit while it is open.

NetServer

new NetServer(opts?: NetOptions, onconnection?: (socket: NetSocket) => void)

Source

Create a NetServer, optionally registering a connection listener.

Overloads:

new NetServer(opts?: NetOptions, onconnection?: (socket: NetSocket) => void)
new NetServer(onconnection: (socket: NetSocket) => void)

Parameters

ParameterTypeDefaultDescription
opts?NetOptionsOptions applied to each accepted socket; readBufferSize defaults to 65536, and allowHalfOpen and pauseOnConnect to false.
onconnection?(socket: NetSocket) => voidCalled on each 'connection' event.

address(): string | TCPSocketAddress | null

Source

Returns the address the server is listening on, or null if it isn't listening.

close(onclose: (err?: Error) => void): this

Source

Stop the server from accepting new connections.

Parameters

ParameterTypeDefaultDescription
onclose(err?: Error) => voidCalled once when the server emits 'close'.

listen

listen(path: string, backlog?: number, opts?: PipeServerListenOptions, onlistening?: () => void): this
Source

Start the server listening on a path over IPC, or a port/host over TCP.

Parameters

ParameterTypeDefaultDescription
pathstringThe path to listen on over IPC.
backlog?numberThe maximum length of the queue of pending connections.
opts?PipeServerListenOptions
onlistening?() => voidCalled once when the server emits 'listening'.

listening: boolean

Source

true if the server is currently listening for connections.

NetServer.ref(): this

Source

Reference the server, keeping the event loop alive while it is listening.

NetServer.unref(): this

Source

Unreference the server, allowing the event loop to exit while it is listening.

Functions

createConnection

createConnection(path: string, opts?: NetOptions & PipeConnectOptions, onconnect?: () => void): NetSocket
Source

Create a NetSocket and connect it over IPC if a path is given, otherwise over TCP.

Parameters

ParameterTypeDefaultDescription
pathstringThe path to connect to over IPC.
opts?NetOptions & PipeConnectOptionsOptions for the socket and connection; if path is set the socket connects over IPC, otherwise over TCP.
onconnect?() => voidCalled when the connection is established.

createServer(opts?: NetOptions, onconnection?: (socket: NetSocket) => void): NetServer

Source

Create a NetServer, optionally registering a connection listener.

Overloads:

createServer(opts?: NetOptions, onconnection?: (socket: NetSocket) => void): NetServer
createServer(onconnection: (socket: NetSocket) => void): NetServer

Parameters

ParameterTypeDefaultDescription
opts?NetOptionsOptions applied to each accepted socket; readBufferSize defaults to 65536, and allowHalfOpen and pauseOnConnect to false.
onconnection?(socket: NetSocket) => voidCalled on each 'connection' event.

Constants and variables

constants

constants: {
  type: { TCP: 1; IPC: 2 }
  state: { UNREFED: number }
}
Source

Connection type and internal state flags used by sockets and servers.

Types

NetOptions

interface NetOptions {
  allowHalfOpen?: boolean
  eagerOpen?: boolean
  readBufferSize?: number
}
Source

Options accepted when constructing a NetSocket or NetServer.

NetSocketEvents

interface NetSocketEvents extends DuplexEvents {
  connect: []
}
Source

Events emitted by a NetSocket, extending the underlying duplex stream's events.

NetSocketConnectOptions

interface NetSocketConnectOptions extends PipeConnectOptions, TCPSocketConnectOptions {}
Source

NetServerEvents

interface NetServerEvents extends EventMap {
  close: []
  connection: [socket: NetSocket]
  error: [err: Error]
  listening: []
}
Source

Events emitted by a NetServer.

NetServerListenOptions

interface NetServerListenOptions extends PipeServerListenOptions, TCPServerListenOptions {}
Source

bare-net/constants

Constants and variables

constants.constants

constants: {
  type: { TCP: 1; IPC: 2 }
  state: { UNREFED: number }
}
Source

Connection type and internal state flags used by sockets and servers.

See also

On this page