Skip to content
Effect Days 2026 Get your ticket

NodeSocketServer

Node socket server adapters for Effect's unstable socket server API.

This module turns node:net TCP or Unix-domain servers, node:tls servers, and ws WebSocket servers into scoped SocketServer.SocketServer services. Use the TCP and TLS constructors when handlers should receive a Socket.Socket backed by a Node net.Socket; use the WebSocket constructors when handlers should receive a Socket.Socket backed by ws and have access to the per-connection WebSocket and IncomingMessage services.

7 exports Added in v4.0.0 Source

Constructors

make

Added in v4.0.0 Source

Creates a scoped TCP SocketServer from a Node net.Server, starts listening with the supplied options, queues pending connections until run is called, and closes the server when the scope ends.

Signature

declare function make(options: ServerOpts & ListenOptions): Effect<{
readonly address: Address;
readonly run: <R, E, _>(handler: (socket: Socket) => Effect<_, E, R>) => Effect<never, SocketServerError, R>;
}, SocketServerError, Scope>

makeTls

Added in v4.0.0 Source

Creates a scoped TLS SocketServer from a Node tls.Server, starts listening with the supplied options, queues pending connections until run is called, and closes the server when the scope ends.

Details

Connections reach run only once their handshake has completed, and the handler receives a Socket.Socket backed by the tls.TLSSocket, which is also provided as NodeSocket.NetSocket. Connections that fail the handshake are destroyed and the server keeps listening.

Signature

declare function makeTls(options: TlsOptions & ListenOptions): Effect<{
readonly address: Address;
readonly run: <R, E, _>(handler: (socket: Socket) => Effect<_, E, R>) => Effect<never, SocketServerError, R>;
}, SocketServerError, Scope>

Creates a scoped WebSocket SocketServer backed by the ws package, providing the WebSocket and its Node IncomingMessage to connection handlers and closing the server when the scope ends.

Signature

declare const makeWebSocket: (options: NodeWS.ServerOptions<typeof NodeWS.WebSocket, typeof Http.IncomingMessage>) => Effect.Effect<SocketServer.SocketServer["Service"], SocketServer.SocketServerError, Scope.Scope>

Layers

layer

Added in v4.0.0 Source

Provides a TCP SocketServer by creating and managing a scoped Node net.Server with the supplied server and listen options.

Signature

declare const layer: (options: Net.ServerOpts & Net.ListenOptions) => Layer.Layer<SocketServer.SocketServer, SocketServer.SocketServerError>

layerTls

Added in v4.0.0 Source

Provides a TLS SocketServer by creating and managing a scoped Node tls.Server with the supplied TLS and listen options.

Signature

declare const layerTls: (options: Tls.TlsOptions & Net.ListenOptions) => Layer.Layer<SocketServer.SocketServer, SocketServer.SocketServerError>

Provides a WebSocket SocketServer backed by the ws package and managed with the supplied server options.

Signature

declare const layerWebSocket: (options: NodeSocket.NodeWS.ServerOptions<typeof NodeSocket.NodeWS.WebSocket, typeof Http.IncomingMessage>) => Layer.Layer<SocketServer.SocketServer, SocketServer.SocketServerError>

Services

Service tag for the Node IncomingMessage associated with the current WebSocket server connection.

Signature

declare class IncomingMessage extends Shape<"@effect/platform-node-shared/NodeSocketServer/IncomingMessage", IncomingMessage, this> {
constructor(_: never);
}