This library implements the WebSocket Web API and is meant to be a drop-in replacement for React Native's WebSocket implementation.
Under the hood, it uses:
- on iOS, built-in WebSocket API (URLSessionWebSocketTask)
- on Android, OkHttp (same as React Native)
During our benchmarking, we found that this WebSocket implementation is 2-3x faster than React Native's implementation under common scenarios and even up to 150x faster when streaming large binary data (such as images).
import { WebSocket, Blob } from 'react-native-fast-io';
// Create a new WebSocket instance
const ws = new WebSocket('wss://echo.websocket.org');
// Using event listeners (recommended)
ws.addEventListener('open', (event) => {
console.log('Connected to server');
ws.send('Hello Server!');
});
// Or using event handlers
ws.onopen = (event) => {
console.log('Connected to server');
};
// Listen to messages
ws.addEventListener('message', (event) => {
console.log('Received:', event.data);
});
// Send different types of data
ws.send('Plain text message');
ws.send(new Blob('string'));
ws.send(new Uint8Array([1, 2, 3, 4]));
// Close the connection when done
ws.close();
Note
When setting binaryType
to 'blob'
, the library will dispatch MessageEvent
s with Blob
objects. This is incompatible with React Native's Blob
.
For more detailed information about the WebSocket API, check out the MDN WebSocket documentation.
- You can send
Blob
,ArrayBuffer
, andArrayBufferView
objects directly. With React Native, they are serialized and sent as base64 strings - Uses direct callbacks per WebSocket instance. React Native broadcasts all events to JS and filters by connection ID, which is less efficient
- It will throw when attempting to close WebSocket connection with a code other than 1000 or outside of range 3000-4999. This is spec compliant behavior and something not supported by React Native's WebSocket implementation