WebSocket
@navios/builder/socket gives messages the same treatment declareEndpoint gives HTTP calls: you declare a topic and its schemas once, and get a typed function that validates what it sends and what it receives. It works over the standard WebSocket, and over any object with the same few methods.
WebSocket or SSE?
If the client only listens, Server-Sent Events is the smaller tool. If it also talks, use a socket.
Create a socket builder
A socket builder holds the connection and hands out send and subscribe functions. It is separate from the HTTP builder and has its own client.
provideClient accepts a handle from declareWebSocket, or anything with the same methods. Sending before a client is provided fails with a NaviosError; subscriptions made before are attached when the client arrives.
Calling provideClient again moves every subscription to the new connection and leaves the old one open. Pass { closePrevious: true } to close it, or close it yourself. Providing the same client again does nothing.
Send messages
defineSend returns a function for one topic. The payload is validated first, and the schema's output is what goes on the wire.
An invalid payload throws a SocketError and nothing is sent. So does a connection that cannot send, because it is not open yet or any more, unless its WebSocket class keeps messages.
Wait for an acknowledgement
Add an ackSchema and the function returns a promise that resolves with the server's validated reply.
The promise rejects with a SocketError when the payload is invalid, the reply fails ackSchema or does not arrive within ackTimeout, or the connection closes first. A send with an ackSchema never throws. The timeout defaults to 30 seconds; set it per topic as above, or for every topic with socketBuilder({ ackTimeout }). Infinity is none.
Subscribe to messages
defineSubscribe returns a function that registers a listener and gives back an unsubscribe function.
A message that fails payloadSchema is skipped and reported to onError; your listener only ever sees valid data. A listener that throws does not stop the other listeners of the topic. Listeners may subscribe and unsubscribe while a message is delivered; one that is added gets the next message.
With a synchronous schema, listeners are called in the same tick the message arrives. With an asynchronous one they are called when its validation resolves, so two messages can be delivered in another order than they arrived in. A message whose validation finishes after the listener unsubscribed is dropped.
Both kinds of function keep their declaration on .config, like an endpoint does.
The connection
declareWebSocket describes a connection the way declareEndpoint describes a request: a URL template, optional urlParamsSchema and querySchema, and optional sub-protocols. Calling the result opens the socket and returns a handle you can give to the builder.
The parameters can be left out when none is required. params are validated even then, so that the defaults and required fields of querySchema apply. Here the schema's output becomes the query string: undefined and null are skipped, a Date is written as an ISO string, and an array repeats its key for each item.
A handle also works on its own:
emit and sendRaw throw a SocketError of kind transport when the socket is not open. (A standard WebSocket that is closed discards what it is asked to send.) The same error is reported to onError. emit cannot wait for a reply; use defineSend with an ackSchema for that.
It also exposes url, connected, state ('connecting' | 'open' | 'closing' | 'closed') and the underlying socket.
open, close, error, message, connect, disconnect and connect_error are connection events on a handle. handle.on('close', ...) never fires for a server message whose topic is close. To listen to a topic with one of those names, use handle.onTopic(topic, listener) or socket.defineSubscribe({ topic }).
When several connections share a host, createWebSocketHandler({ baseUrl, onError }) returns a declareWebSocket with the base URL applied.
A connection that is lost stays closed. To reconnect, listen for disconnect, open another connection and pass it to provideClient.
Another WebSocket class
createWebSocketHandler({ WebSocket }) takes the class connections are opened with: for a runtime without a global WebSocket, such as Node.js before version 22, or to send headers with the handshake, which the standard class cannot.
What becomes of a message while the connection is not open is up to your class. A standard WebSocket would discard it, so the builder refuses to send it and throws. reconnecting-websocket keeps it until it is connected again. ws discards it once it is closed, which is reported to onError, but not thrown.
urlParamsSchema and querySchema must validate synchronously, because the connection opens synchronously.
Wire format
By default every message is a JSON array:
A declareWebSocket handle sends each frame as one JSON text message and reads incoming frames the same way.
To talk to a backend with a different envelope, replace both directions:
When parseMessage returns an ackId that a send is still waiting for, the message settles that send and is not delivered to subscribers. That is how acknowledgements work in a custom format.
Another transport
The client of a socket builder is an object with three methods, described by the SocketClient type. Implement them to send the frames over something other than declareWebSocket, or to use a fake in tests:
emit(topic, frame)sends a frame. With a methodsendRaw(topic, frame)the builder calls that instead.on(event, listener)andoff(event, listener)add and remove listeners. The builder listens tomessage, called with every incoming frame; todisconnect, after which it gives up waiting for acknowledgements; and, while it waits for one, to__navios_ack:<ackId>.
Socket.IO is not supported: a stock Socket.IO client neither sends nor receives these frames.
Errors
Every failure is a SocketError. Send functions throw it or reject with it; failures on the receiving side have no caller, so they only reach onError.
A SocketError carries kind, topic, message, and where they apply url (the connection URL, when the client has one), issues ({ message, path? }, without the validated value), rawData (the value that failed validation or parsing) and cause (what the schema library reported, or whatever was thrown). toJSON() returns the same fields without cause.
One asynchronous-schema rule follows from the return types: a send without ackSchema returns nothing, so it cannot wait for a payloadSchema that validates asynchronously and throws a validation error instead. Everywhere else asynchronous schemas are awaited.
The onError hook
socketBuilder({ onError }) is called once for every row in the table above, with the same instance a send function throws.
The hook is for telemetry. An exception thrown inside it is caught and logged, and never changes how a message is handled. createWebSocketHandler({ onError }) has the same contract for the handle itself: a listener registered with handle.on() that throws, or a frame that could not be sent, is reported as kind transport.
Related: Realtime updates pushes these messages into the React Query cache, and the API reference lists every export of this entry point.