Skip to content

NodeSocketServer

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

This module turns node:net TCP or Unix-domain servers and ws WebSocket servers into scoped SocketServer.SocketServer services. Use the TCP 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.

5 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 const make: (...args: [options: any]) => 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>;

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);
}