Skip to main content
Version: v8.0.0

Adapter

Read the API Reference »

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
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.

OptionTypeDefaultDescription
autoConnectbooleantrueAutomatically connect when the socket is created.
protocolsstring[]Subprotocols passed to the WebSocket constructor.
heartbeatbooleanEnable heartbeat messages to keep the connection alive.
heartbeatMessagestringThe message payload sent for heartbeats.
pingTimeoutnumberTimeout (ms) to wait for a ping response.
pongTimeoutnumberTimeout (ms) to wait for a pong response.
const socket = createSocketClient({
url: "ws://localhost:3000",
adapterOptions: {
autoConnect: false,
protocols: ["v1"],
heartbeat: true,
heartbeatMessage: "ping",
},
});
WebsocketAdapter
Read the API Reference for the WebsocketAdapter class.

Server Sent Events (SSE) Adapter

The ServerSentEventsAdapter is used to communicate with the Server Sent Events (SSE) server.

SSE is receive-only

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
import { ServerSentEventsAdapter } from "@hyper-fetch/sockets"

import { createSocketClient, ServerSentEventsAdapter } from "@hyper-fetch/sockets";

const socket = createSocketClient({
url: "http://localhost:3000",
adapter: ServerSentEventsAdapter(),
});

Options

OptionTypeDefaultDescription
autoConnectbooleantrueAutomatically connect when the socket is created.
eventSourceInitEventSourceInitOptions passed to the underlying EventSource constructor (e.g., withCredentials).
const socket = createSocketClient({
url: "http://localhost:3000",
adapter: ServerSentEventsAdapter(),
adapterOptions: {
autoConnect: false,
eventSourceInit: { withCredentials: true },
},
});
ServerSentEventsAdapter
Read the API Reference for the ServerSentEventsAdapter class.

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.


See More

Socket
Learn more about the Socket class and its configuration options.
Emitter
Learn more about the Emitter class and its configuration options.
Listener
Learn more about the Listener class and its configuration options.
Firebase Realtime Database
Learn more about the Firebase Realtime Database integration.