{ } qjs-lws

LWSContext

A wrapper around struct lws_context. The context owns the default vhost, the listener (if any), the protocol callback table, and any mounts. Implemented in lws-context.c.

Construction

const ctx = new LWSContext(info);

createServer(info) (exported from 'lws') is a thin alias for the same constructor call — JS_CallConstructor on LWSContext under the hood — meant for call sites where info.port is set, i.e. the context will listen. It's purely a naming convenience; the two are otherwise identical, and either is fine for a client-only context too.

info is a plain object mapped to struct lws_context_creation_info. Properties whose JS name is camelCase are also accepted in their underscore form (e.g. vhostName or vhost_name), thanks to the camelize lookup in js-utils.c.

Whenever a property is not provided and port is missing, the context is created with port = CONTEXT_PORT_NO_LISTEN (no listener — suitable for pure clients).

Common properties

PropertyC fieldDescription
portportTCP port to listen on (CONTEXT_PORT_NO_LISTEN to disable)
ifaceifaceInterface address or name to bind
vhostName / vhost_namevhost_nameDefault vhost name
protocolsprotocolsArray of protocol handler objects, see protocols.md
mountsmountsHTTP mount points (array or { '/path': mount } object)
headersheaderslws_protocol_vhost_options — default response headers
pvopvoPer-vhost options
rejectServiceKeywordsreject_service_keywordsList of keywords to reject
httpProxyAddresshttp_proxy_addressHTTP proxy host
httpProxyPorthttp_proxy_portHTTP proxy port
keepaliveTimeoutkeepalive_timeoutTCP keepalive seconds
logFilepathlog_filepathServer log file
serverStringserver_stringServer: header
errorDocument404error_document_404URL for 404 errors
vhListenSockfdvh_listen_sockfdPre-allocated listen fd
defaultLogleveldefault_loglevelPer-context log level
optionsoptionsOR-mask of LWS_SERVER_OPTION_* constants
listenAcceptRolelisten_accept_roleRole applied when FALLBACK_TO_APPLY_LISTEN_ACCEPT_CONFIG or ADOPT_APPLY_LISTEN_ACCEPT_CONFIG is set (e.g. 'raw-skt')
listenAcceptProtocollisten_accept_protocolProtocol name applied with the role. Can be anywhere in the protocols array.
asyncDnsServersasync_dns_serversArray of DNS server strings (built with LWS_WITH_SYS_ASYNC_DNS)

TLS properties

Built only when libwebsockets is compiled with LWS_WITH_TLS. Each of serverSsl* / clientSsl* may be either a file path string (*_filepath) or an ArrayBuffer containing the certificate or key material (*_mem / *_mem_len set accordingly). The str_or_buf_property() helper picks the right field automatically:

PropertyC field(s)
sslPrivateKeyPasswordssl_private_key_password
serverSslCertssl_cert_filepath or server_ssl_cert_mem
serverSslPrivateKeyssl_private_key_filepath or server_ssl_private_key_mem
serverSslCassl_ca_filepath or server_ssl_ca_mem
sslCipherListssl_cipher_list
tls13PlusCipherList / tls1_3_plus_cipher_listtls1_3_plus_cipher_list
clientSslPrivateKeyPasswordclient_ssl_private_key_password
clientSslCertclient_ssl_cert_filepath or client_ssl_cert_mem
clientSslPrivateKeyclient_ssl_private_key_filepath or client_ssl_key_mem
clientSslCaclient_ssl_ca_filepath or client_ssl_ca_mem
clientSslCipherListclient_ssl_cipher_list
clientTls13PlusCipherListclient_tls_1_3_plus_cipher_list

SOCKS5

Built only with LWS_WITH_SOCKS5:

PropertyC field
socksProxyAddresssocks_proxy_address
socksProxyPortsocks_proxy_port

WebSocket extensions

PropertyC fieldDescription
permessageDeflateextensionsBoolean. When true (and the build has LWS_ROLE_WS), installs the permessage-deflate extension with the parameters client_no_context_takeover; client_max_window_bits. Off by default — decompression-chunk boundaries don't line up with WS message/frame boundaries (lws_is_final_fragment() tracks the former), so a large message sent with compression on can arrive as several separate onReceive/onClientReceive calls that JS has no reliable way to tell belong together. Only opt in if you don't depend on message/fragment boundaries, or you handle reassembly yourself.

Instance methods

MethodNotes
destroy()Calls lws_context_destroy(); sets internal pointer to NULL. Returns true.
getVhostByName(name)Returns the matching LWSVhost or undefined.
adoptSocket(fd)Adopts an existing OS socket; returns LWSSocket. Throws if fd is already adopted.
adoptSocketReadbuf(fd, buf)Same as above but with pre-buffered read data.
cancelService()lws_cancel_service() and cleans up io handlers.
clientConnect(uriOrInfo [, info])Initiates an outbound client connection. See below.
getRandom(buf)Fills the ArrayBuffer with libwebsockets random bytes.
asyncDnsServerAdd(addr)LWSSockAddr46-style; returns int.
asyncDnsServerRemove(addr)Removes a previously added DNS server.
wsiFromFd(fd)Looks up the LWSSocket for an OS fd, or undefined.
createUdp(options)Creates (and, unless bind is set, connects) a UDP socket via lws_create_adopt_udp(). Built only with LWS_WITH_UDP. See below.

clientConnect

Two call styles:

ctx.clientConnect('https://example.com/path');
ctx.clientConnect('wss://example.com/ws', { protocol: 'chat' });
ctx.clientConnect({
  address: 'example.com',
  port: 443,
  path: '/foo',
  ssl: true,
  protocol: 'http',
});

Recognised properties on the info object:

PropertyC field
contextcontext (auto-filled with this.ctx)
addressaddress
portport
sslConnection / ssl_connectionssl_connection (OR'd)
sslshortcut: true → standard insecure SSL flags; number → OR'd into ssl_connection
pathpath
hosthost
originorigin
protocolprotocol (wire WebSocket subprotocol, or "http"/"raw"/...)
methodmethod (e.g. 'GET', 'POST', 'RAW')
ifaceiface
localPort / local_portlocal_port
localProtocolName / local_protocol_namelocal_protocol_name (which JS protocol callback to invoke)
alpnalpn (e.g. 'h2,http/1.1')
keepWarmSecs / keep_warm_secskeep_warm_secs
authUsername / auth_usernameauth_username
authPassword / auth_passwordauth_password

When a URI string is passed, the scheme decides: http*method = 'GET'; https/wssssl_connection is set to a permissive default (`USE_SSL | ALLOW_SELFSIGNED | ALLOW_EXPIRED | SKIP_SERVER_CERT_HOSTNAME_CHECK | ALLOW_INSECURE`).

Returns the freshly created LWSSocket. Even on failure the socket object exists; you observe failures via the protocol's onClientConnectionError callback.

createUdp

UDP is connectionless, so both "bind a listening socket that receives datagrams from any peer" and "create a socket pre-connected to one fixed remote peer" go through this single entry point, distinguished by whether address is given / bind is set:

const listener = ctx.createUdp({ protocol: 'udp-echo', bind: true, port: 9002 });
const client = ctx.createUdp({ protocol: 'udp-echo', address: '127.0.0.1', port: 9002 });

Recognised properties on the options object:

PropertyC field / meaning
protocolName of a protocol on the vhost (required)
addressRemote address to pre-connect to; omitted (or bind: true) binds a listener on any interface instead
portUDP port
ifaceInterface address or name to bind
vhostVhost name to look the protocol up on (defaults to the context's default vhost)
bindBoolean — force LWS_CAUDP_BIND even when address is given
broadcastBoolean — sets LWS_CAUDP_BROADCAST
parentWsi / parent_wsiAn existing LWSSocket to link the new UDP wsi under (lws_create_adopt_udp()'s parent_wsi argument)

A single UDP wsi, unlike TCP, never gets one child wsi per peer — see onRawRx's extra LWSSockAddr46 argument (callbacks.md) for how a listener tells its many peers apart, and wsi.write(data, sockaddr) (see LWSSocket.md) for targeting a reply back at one of them.

Throws TypeError if protocol is missing, InternalError if the named vhost doesn't exist or lws_create_adopt_udp() fails.

Instance accessors (read-only)

PropertyReturns
hostnameCanonical hostname (lws_canonical_hostname)
deprecatedBoolean — context replaced by a newer one
euidEffective uid
egidEffective gid
protocolsArray of protocol descriptor objects (see protocols.md)

The info property is also set during construction — it's the original options object, kept alive for the lifetime of the context (JS_PROP_CONFIGURABLE).

Lifecycle

  • The context is finalised when the JS object is garbage collected, which triggers lws_context_destroy() automatically.
  • destroy() is safe to call early; subsequent method calls throw InternalError("LWSContext internal lws_context has been destroyed").
  • The constructor may trigger callbacks before returning, because lws_create_context() is the last step performed.