Reliable WebSocket connectivity for web and React Native applications.
Project details
steady-socket is a lightweight library designed to maintain stable WebSocket connections in web applications and React Native. It handles connection issues seamlessly, reconnecting automatically while managing subscriptions and ensuring efficient data flow without burdening the server. Ideal for real-time applications needing consistent updates.
steady-socket is a robust WebSocket client library designed to maintain a persistent connection for web applications and React Native environments. It addresses common issues encountered with WebSocket connections, such as disruptions caused by network changes and server restarts, by implementing intelligent reconnection strategies and subscription management.
Here is a basic setup example to demonstrate how to create and manage a WebSocket connection using steady-socket:
import { createSocket } from 'steady-socket';
type Incoming = { type: 'pong' } | { type: 'event'; topic: string; payload: unknown };
type Outgoing = { type: 'ping' } | { type: 'subscribe' | 'unsubscribe'; topic: string };
const socket = createSocket<Incoming, Outgoing>({
url: async () => `wss://api.example.com/ws?token=${await getToken()}`,
heartbeat: {
interval: 15000, // ping every 15 seconds
timeout: 5000, // close if no pong in 5 seconds
ping: () => ({ type: 'ping' }),
isPong: (msg) => msg.type === 'pong',
},
reconnect: {
minDelay: 500,
maxDelay: 30000,
factor: 2,
jitter: 'full',
},
});
socket.connect();
idle, connecting, open, reconnecting, closed), allowing developers to manage user interfaces based on connection status.Explore a live demo at steady-socket web demo, where various connection scenarios can be tested interactively.
steady-socket provides a powerful, lightweight solution for applications relying on real-time data, ensuring reliability and easier development in both web and mobile platforms. Its thoughtful design allows developers to focus on building features rather than managing WebSocket complexities.
Comments
0Start the conversation
Share the first comment.