Adapter
Adapter is a class that specify the way how to communicate with the server. It defines the way how to send and receive
data, how to handle errors, how to handle reconnects, etc. Currently supporting connection types like Websocket,
Server Sent Events (SSE) or integrations like Firebase Realtime Database.
Built in Adapters
There are two built in adapters - WebsocketAdapter and ServerSentEventsAdapter providing basic functionality for the
Websocket and Server Sent Events (SSE) connection types.
Websocket Adapter
The default adapter is used to communicate with the Websocket server.
import { WebsocketAdapter } from "@hyper-fetch/sockets"
import { createSocketClient, WebsocketAdapter } from "@hyper-fetch/sockets";
const socket = createSocketClient({
url: "ws://localhost:3000",
adapter: WebsocketAdapter(),
});
Options
You can pass adapter-specific options via the adapterOptions constructor option on Socket.
| Option | Type | Default | Description |
|---|---|---|---|
autoConnect | boolean | true | Automatically connect when the socket is created. |
protocols | string[] | — | Subprotocols passed to the WebSocket constructor. |
heartbeat | boolean | — | Enable heartbeat messages to keep the connection alive. |
heartbeatMessage | string | — | The message payload sent for heartbeats. |
pingTimeout | number | — | Timeout (ms) to wait for a ping response. |
pongTimeout | number | — | Timeout (ms) to wait for a pong response. |
const socket = createSocketClient({
url: "ws://localhost:3000",
adapterOptions: {
autoConnect: false,
protocols: ["v1"],
heartbeat: true,
heartbeatMessage: "ping",
},
});
Server Sent Events (SSE) Adapter
The ServerSentEventsAdapter is used to communicate with the Server Sent Events (SSE) server.
The SSE adapter does not support emitting events. Calling emit on an emitter with this adapter will throw an
error. Use the WebsocketAdapter if you need bidirectional communication.
import { ServerSentEventsAdapter } from "@hyper-fetch/sockets"
import { createSocketClient, ServerSentEventsAdapter } from "@hyper-fetch/sockets";
const socket = createSocketClient({
url: "http://localhost:3000",
adapter: ServerSentEventsAdapter(),
});
Options
| Option | Type | Default | Description |
|---|---|---|---|
autoConnect | boolean | true | Automatically connect when the socket is created. |
eventSourceInit | EventSourceInit | — | Options passed to the underlying EventSource constructor (e.g., withCredentials). |
const socket = createSocketClient({
url: "http://localhost:3000",
adapter: ServerSentEventsAdapter(),
adapterOptions: {
autoConnect: false,
eventSourceInit: { withCredentials: true },
},
});
Custom Adapter
There is nothing to prevent you from changing this and creating the adapter you need. Thanks to event communication, you
can set your own adapter as you wish by only fulfilling the typescript requirements. This way you can create your own
adapters for existing libraries like socket.io. Refer to the SocketAdapter API reference for the full contract your
custom adapter must satisfy.
