Socket.ts
Socket.ts overview
Section titled “Socket.ts overview”Models bidirectional socket connections in Effect.
The Socket service runs handlers for binary, string, or raw frames and
provides a scoped writer for outgoing bytes, text, or close events. This
module also includes socket errors, channel adapters, WebSocket layers, and
transform-stream-backed sockets.
Since v4.0.0
Exports Grouped by Category
Section titled “Exports Grouped by Category”- combinators
- constructors
- errors
- fiber refs
- guards
- layers
- models
- predicates
- refinements
- services
- type IDs
combinators
Section titled “combinators”toChannel
Section titled “toChannel”Converts a Socket into a binary Channel, encoding incoming string frames
as UTF-8 bytes.
Signature
declare const toChannel: <IE>( self: Socket) => Channel.Channel< NonEmptyReadonlyArray<Uint8Array>, SocketError | IE, void, NonEmptyReadonlyArray<Uint8Array | string | CloseEvent>, IE>Since v4.0.0
toChannelMap
Section titled “toChannelMap”Converts a Socket into a bidirectional Channel, mapping incoming string
or binary frames and writing outgoing frame batches to the socket.
Signature
declare const toChannelMap: <IE, A>( self: Socket, f: (data: Uint8Array | string) => A) => Channel.Channel< NonEmptyReadonlyArray<A>, SocketError | IE, void, NonEmptyReadonlyArray<Uint8Array | string | CloseEvent>, IE>Since v4.0.0
toChannelString
Section titled “toChannelString”Converts a Socket into a string Channel, decoding binary frames with the
optional text encoding.
Signature
declare const toChannelString: { ( encoding?: string | undefined ): <IE>( self: Socket ) => Channel.Channel< NonEmptyReadonlyArray<string>, SocketError | IE, void, NonEmptyReadonlyArray<Uint8Array | string | CloseEvent>, IE > <IE>( self: Socket, encoding?: string | undefined ): Channel.Channel< NonEmptyReadonlyArray<string>, SocketError | IE, void, NonEmptyReadonlyArray<Uint8Array | string | CloseEvent>, IE >}Since v4.0.0
toChannelWith
Section titled “toChannelWith”Creates a Socket to binary Channel adapter with a fixed upstream error
type.
Signature
declare const toChannelWith: <IE = never>() => ( self: Socket) => Channel.Channel< NonEmptyReadonlyArray<Uint8Array>, SocketError | IE, void, NonEmptyReadonlyArray<Uint8Array | string | CloseEvent>, IE>Since v4.0.0
constructors
Section titled “constructors”fromTransformStream
Section titled “fromTransformStream”Builds a Socket from a scoped InputTransformStream, reading incoming
chunks through socket handlers and writing outgoing chunks to the writable
stream, encoding strings as UTF-8 and using close-code classification for
CloseEvent values.
Signature
declare const fromTransformStream: <R>( acquire: Effect.Effect<InputTransformStream, SocketError, R>, options?: { readonly closeCodeIsError?: (code: number) => boolean }) => Effect.Effect<Socket, never, Exclude<R, Scope.Scope>>Since v4.0.0
fromWebSocket
Section titled “fromWebSocket”Builds a Socket from a scoped WebSocket acquisition effect, waiting for the
socket to open, dispatching message handlers in fibers, and translating
open, read, and close events into SocketError values.
Signature
declare const fromWebSocket: <RO>( acquire: Effect.Effect<globalThis.WebSocket, SocketError, RO>, options?: | { readonly closeCodeIsError?: ((code: number) => boolean) | undefined readonly openTimeout?: Duration.Input | undefined } | undefined) => Effect.Effect<Socket, never, Exclude<RO, Scope.Scope>>Since v4.0.0
Constructs a Socket from a raw read loop and scoped writer, deriving binary
and string read loops when they are not provided.
Signature
declare const make: (options: { readonly runRaw: <_, E, R>( handler: (_: string | Uint8Array) => Effect.Effect<_, E, R> | void, options?: { readonly onOpen?: Effect.Effect<void> | undefined } ) => Effect.Effect<void, SocketError | E, R> readonly run?: <_, E, R>( handler: (_: Uint8Array) => Effect.Effect<_, E, R> | void, options?: { readonly onOpen?: Effect.Effect<void> | undefined } ) => Effect.Effect<void, SocketError | E, R> readonly runString?: <_, E, R>( handler: (_: string) => Effect.Effect<_, E, R> | void, options?: { readonly onOpen?: Effect.Effect<void> | undefined } ) => Effect.Effect<void, SocketError | E, R> readonly writer: Effect.Effect< (chunk: Uint8Array | string | CloseEvent) => Effect.Effect<void, SocketError>, never, Scope.Scope >}) => SocketSince v4.0.0
makeChannel
Section titled “makeChannel”Creates a binary socket Channel from the Socket service in the
environment.
Signature
declare const makeChannel: <IE = never>() => Channel.Channel< NonEmptyReadonlyArray<Uint8Array>, SocketError | IE, void, NonEmptyReadonlyArray<Uint8Array | string | CloseEvent>, IE, unknown, Socket>Since v4.0.0
makeWebSocket
Section titled “makeWebSocket”Creates a Socket backed by a WebSocketConstructor, acquiring the
WebSocket for each run and using the close-code classifier to decide which
closes fail the run.
Signature
declare const makeWebSocket: ( url: string | Effect.Effect<string>, options?: { readonly closeCodeIsError?: ((code: number) => boolean) | undefined readonly openTimeout?: Duration.Input | undefined readonly protocols?: string | Array<string> | undefined }) => Effect.Effect<Socket, never, WebSocketConstructor>Since v4.0.0
makeWebSocketChannel
Section titled “makeWebSocketChannel”Creates a binary Channel backed by a WebSocket URL, requiring a
WebSocketConstructor service.
Signature
declare const makeWebSocketChannel: <IE = never>( url: string, options?: { readonly closeCodeIsError?: (code: number) => boolean }) => Channel.Channel< NonEmptyReadonlyArray<Uint8Array>, SocketError | IE, void, NonEmptyReadonlyArray<Uint8Array | string | CloseEvent>, IE, unknown, WebSocketConstructor>Since v4.0.0
errors
Section titled “errors”SocketCloseError (class)
Section titled “SocketCloseError (class)”Typed error for a socket close event, carrying the close code and optional close reason.
Signature
declare class SocketCloseErrorSince v4.0.0
filterClean (static method)
Section titled “filterClean (static method)”Separates clean socket close errors from errors that should remain failures.
Signature
declare const filterClean: (isClean: (code: number) => boolean) => <E>(u: E) => Result.Result<SocketCloseError, E>Since v4.0.0
SocketError (class)
Section titled “SocketError (class)”Tagged error that wraps socket read, write, open, and close failures while preserving the underlying reason.
Signature
declare class SocketError { constructor(props: { readonly reason: SocketReadError | SocketWriteError | SocketOpenError | SocketCloseError })}Since v4.0.0
is (static method)
Section titled “is (static method)”Returns true when the value is a SocketError.
Signature
declare const is: (u: unknown) => u is SocketErrorSince v4.0.0
[SocketErrorTypeId] (property)
Section titled “[SocketErrorTypeId] (property)”Marks this value as a socket error wrapper for runtime guards.
Signature
readonly [SocketErrorTypeId]: "~effect/socket/Socket/SocketError"Since v4.0.0
message (property)
Section titled “message (property)”Signature
readonly message: stringSocketErrorReason
Section titled “SocketErrorReason”Schema for all socket-specific error reasons.
Signature
declare const SocketErrorReason: Schema.Union< readonly [typeof SocketReadError, typeof SocketWriteError, typeof SocketOpenError, typeof SocketCloseError]>Since v4.0.0
SocketErrorReason (type alias)
Section titled “SocketErrorReason (type alias)”Union of socket-specific read, write, open, and close error reasons.
Signature
type SocketErrorReason = SocketReadError | SocketWriteError | SocketOpenError | SocketCloseErrorSince v4.0.0
SocketOpenError (class)
Section titled “SocketOpenError (class)”Typed error for failures that occur while opening a socket, including unknown open failures and open timeouts.
Signature
declare class SocketOpenErrorSince v4.0.0
SocketReadError (class)
Section titled “SocketReadError (class)”Typed error for failures that occur while reading from a socket.
Signature
declare class SocketReadErrorSince v4.0.0
message (property)
Section titled “message (property)”Default message used for socket read failures.
Signature
readonly message: "An error occurred during Read"Since v4.0.0
SocketWriteError (class)
Section titled “SocketWriteError (class)”Typed error for failures that occur while writing to a socket.
Signature
declare class SocketWriteErrorSince v4.0.0
message (property)
Section titled “message (property)”Default message used for socket write failures.
Signature
readonly message: "An error occurred during Write"Since v4.0.0
fiber refs
Section titled “fiber refs”SendQueueCapacity
Section titled “SendQueueCapacity”Context reference for socket send queue capacity, defaulting to 16.
Signature
declare const SendQueueCapacity: Context.Reference<number>Since v4.0.0
guards
Section titled “guards”isSocket
Section titled “isSocket”Returns true when a value is a Socket.
Signature
declare const isSocket: (u: unknown) => u is SocketSince v4.0.0
layers
Section titled “layers”layerWebSocket
Section titled “layerWebSocket”Layer that provides a Socket service backed by a WebSocket URL or URL
effect.
Signature
declare const layerWebSocket: ( url: string | Effect.Effect<string>, options?: | { readonly closeCodeIsError?: ((code: number) => boolean) | undefined readonly openTimeout?: Duration.Input | undefined readonly protocols?: string | Array<string> | undefined } | undefined) => Layer.Layer<Socket, never, WebSocketConstructor>Since v4.0.0
layerWebSocketConstructorGlobal
Section titled “layerWebSocketConstructorGlobal”Layer that provides WebSocketConstructor using globalThis.WebSocket.
Signature
declare const layerWebSocketConstructorGlobal: Layer.Layer<WebSocketConstructor, never, never>Since v4.0.0
models
Section titled “models”CloseEvent (class)
Section titled “CloseEvent (class)”Represents a socket close event value carrying a close code and optional reason.
Signature
declare class CloseEvent { constructor(code = 1000, reason?: string)}Since v4.0.0
toString (method)
Section titled “toString (method)”Formats the close code and optional reason for display.
Signature
declare const toString: () => stringSince v4.0.0
[CloseEventTypeId] (property)
Section titled “[CloseEventTypeId] (property)”Marks this value as a socket close event for runtime guards.
Signature
readonly [CloseEventTypeId]: "~effect/socket/Socket/CloseEvent"Since v4.0.0
code (property)
Section titled “code (property)”Signature
readonly code: numberreason (property)
Section titled “reason (property)”Signature
readonly reason: string | undefinedInputTransformStream (interface)
Section titled “InputTransformStream (interface)”Readable and writable stream pair used to adapt transform-style streams into
a Socket.
Signature
export interface InputTransformStream { readonly readable: ReadableStream<Uint8Array> | ReadableStream<string> | ReadableStream<Uint8Array | string> readonly writable: WritableStream<Uint8Array>}Since v4.0.0
Socket (interface)
Section titled “Socket (interface)”Effect-based socket abstraction for running string or binary read handlers and obtaining a scoped writer for outgoing frames and close events.
Signature
export interface Socket { readonly [TypeId]: typeof TypeId readonly run: <_, E = never, R = never>( handler: (_: Uint8Array) => Effect.Effect<_, E, R> | void, options?: { readonly onOpen?: Effect.Effect<void> | undefined } ) => Effect.Effect<void, SocketError | E, R> readonly runString: <_, E = never, R = never>( handler: (_: string) => Effect.Effect<_, E, R> | void, options?: { readonly onOpen?: Effect.Effect<void> | undefined } ) => Effect.Effect<void, SocketError | E, R> readonly runRaw: <_, E = never, R = never>( handler: (_: string | Uint8Array) => Effect.Effect<_, E, R> | void, options?: { readonly onOpen?: Effect.Effect<void> | undefined } ) => Effect.Effect<void, SocketError | E, R> readonly writer: Effect.Effect< (chunk: Uint8Array | string | CloseEvent) => Effect.Effect<void, SocketError>, never, Scope.Scope >}Since v4.0.0
predicates
Section titled “predicates”defaultCloseCodeIsError
Section titled “defaultCloseCodeIsError”Default close-code classifier that treats every socket close code as an error.
Signature
declare const defaultCloseCodeIsError: (_code: number) => booleanSince v4.0.0
refinements
Section titled “refinements”isCloseEvent
Section titled “isCloseEvent”Returns true when a value is a CloseEvent.
Signature
declare const isCloseEvent: (u: unknown) => u is CloseEventSince v4.0.0
isSocketError
Section titled “isSocketError”Returns true when a value is a SocketError.
Signature
declare const isSocketError: (u: unknown) => u is SocketErrorSince v4.0.0
services
Section titled “services”Socket
Section titled “Socket”Service tag for bidirectional socket transports.
When to use
Use to access or provide the socket implementation used by programs that read and write frames through the Effect environment.
Signature
declare const Socket: Context.Service<Socket, Socket>Since v4.0.0
WebSocket (class)
Section titled “WebSocket (class)”Context service for the active WebSocket instance available while a
WebSocket-backed socket run is handling events.
Signature
declare class WebSocketSince v4.0.0
WebSocketConstructor (class)
Section titled “WebSocketConstructor (class)”Context service for constructing WebSocket instances from a URL and
optional protocols.
Signature
declare class WebSocketConstructorSince v4.0.0
type IDs
Section titled “type IDs”SocketErrorTypeId
Section titled “SocketErrorTypeId”Runtime type identifier attached to SocketError values.
Signature
declare const SocketErrorTypeId: "~effect/socket/Socket/SocketError"Since v4.0.0
SocketErrorTypeId (type alias)
Section titled “SocketErrorTypeId (type alias)”Type-level identifier used to mark SocketError values.
Signature
type SocketErrorTypeId = "~effect/socket/Socket/SocketError"Since v4.0.0
TypeId
Section titled “TypeId”Runtime type identifier attached to Socket services.
Signature
declare const TypeId: "~effect/socket/Socket"Since v4.0.0