LogoPear Docs
ReferencesBareModules

bare-tcp

Reference for bare-tcp: native TCP sockets and servers for Bare, with an API close to the Node.js net module.

stable

bare-tcp provides native TCP sockets and servers for Bare. The shape mirrors the Node.js net module. Sockets are bare-stream duplex streams. It's a native addon and requires Bare >=1.16.0.

npm i bare-tcp

Usage

const tcp = require('bare-tcp')

const server = tcp.createServer((socket) => {
  socket.write('hello\n')
  socket.end()
})

server.listen(3000, () => {
  const socket = tcp.createConnection(3000)
  socket.on('data', (data) => console.log(data.toString()))
})

API

Socket

const socket = new tcp.Socket([options])

Create a TCP socket (a duplex stream).

socket.connect(port[, host[, options]][, onconnect])

Connect to a remote endpoint. host defaults to localhost.

socket.setKeepAlive([enable][, delay]) · socket.setNoDelay([enable]) · socket.setTimeout(ms[, ontimeout])

Tune keep-alive, Nagle's algorithm, and the idle timeout.

socket.ref() · socket.unref()

Keep the event loop alive for this socket, or release it.

Properties include socket.connecting, socket.pending, socket.readyState, socket.timeout, and the local/remote address, family, and port. Sockets emit connect, lookup, and timeout (in addition to the usual stream events).

Server

const server = tcp.createServer([options][, onconnection])

Create a TCP server. onconnection is added as a connection listener.

server.listen([port[, host[, backlog[, options]]]][, onlistening])

Start listening for connections.

server.address() · server.close([onclose]) · server.ref() · server.unref()

Report the bound address, stop listening, and manage event-loop references.

Properties include server.listening, server.closing, and server.connections. Servers emit listening, connection, close, error, and lookup.

Helpers

const socket = tcp.createConnection(port[, host[, options]][, onconnect])

Shorthand for creating a Socket and calling connect().

tcp.isIP(host) · tcp.isIPv4(host) · tcp.isIPv6(host)

Classify a string as an IPv4/IPv6 address.

tcp.constants · tcp.errors

Constant and error tables.

Builds on bare-dns, bare-events, and bare-stream (see Bare modules).

See also

On this page