When your WebSockets hit the Nitro boost 🏎️💨
npm install react-native-fast-ws --save
[!NOTE]
This package requires React Native 0.76 or higher. You must also install and configure Nitro Modules to use this package.
[!WARNING]
This library is still under development. Use at your own risk. The API is experimental and subject to change. This project explores new ideas and approaches to WebSocket and Blob APIs, thanks to Nitro, and its scope may change.
This library implements the WebSocket Web API and is meant to be a drop-in replacement for React Native's WebSocket implementation.
import { WebSocket } from 'react-native-fast-ws';
// 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 experimental and incompatible with React Native's Blob
. We're working on a more comprehensive API for this and other networking modules. If you would like to stay informed, let us know.
For more detailed information about the WebSocket API, check out the MDN WebSocket documentation.
Differences with React Native
- Can create
Blob
s from ArrayBuffer
, ArrayBufferView
too. With React Native, you can only create Blob from string or another Blob
- Each
Blob
has text()
, arrayBuffer()
, and stream()
methods. With React Native, you can only access the contents of Blob
via custom data
field
-
Blob
does not depend on a native module. Uses ArrayBuffer
s natively, thanks to Nitro Modules. With React Native, Blob uses RCTBlobManager
under the hood to create and hold references to the data.
-
Blob
is automatically deallocated when no longer in use. You don't have to close()
it manually
- You can send
Blob
, ArrayBuffer
, and ArrayBufferView
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
[!NOTE]
Benchmarks shown below operate in controlled environments and may not accurately reflect real-world performance. Results can vary significantly based on factors such as:
- Device model and OS version
- Network conditions and latency
- Message payload size and type
- Application state and concurrent operations
These benchmarks are provided to demonstrate theoretical performance benefits and should be used as a general reference rather than absolute performance indicators.
You can browse the benchmark code here and check each message payload here.
Small JSON payload - Text
Android |
iOS |
100x |
FastWS |
RN |
|
Out |
0.81ms |
1.40ms |
1.73x |
In |
2.67ms |
20.76ms |
7.76x |
10000x |
FastWS |
RN |
|
Out |
3.91ms |
14.06ms |
3.60x |
In |
17.26ms |
208.32ms |
12.07x |
10000x |
FastWS |
RN |
|
Out |
32.86ms |
176.36ms |
5.37x |
In |
126.74ms |
2096.35ms |
16.54x |
|
100x |
FastWS |
RN |
|
Out |
0.20ms |
0.66ms |
3.34x |
In |
4.24ms |
11.79ms |
2.78x |
1000x |
FastWS |
RN |
|
Out |
2.12ms |
4.23ms |
1.99x |
In |
27.42ms |
81.75ms |
2.98x |
10000x |
FastWS |
RN |
|
Out |
13.70ms |
40.52ms |
2.96x |
In |
227.62ms |
812.45ms |
3.57x |
|
Small JSON payload - Binary
Android |
iOS |
100x |
FastWS |
RN |
|
Out |
1.29ms |
1.32ms |
1.02x |
In |
3.71ms |
23.42ms |
6.31x |
1000x |
FastWS |
RN |
|
Out |
15.80ms |
27.34ms |
1.73x |
In |
22.58ms |
212.51ms |
9.41x |
10000x |
FastWS |
RN |
|
Out |
122.48ms |
349.38ms |
2.85x |
In |
234.25ms |
2128.34ms |
9.09x |
|
100x |
FastWS |
RN |
|
Out |
0.48ms |
1.41ms |
2.92x |
In |
4.40ms |
12.24ms |
2.79x |
1000x |
FastWS |
RN |
|
Out |
4.36ms |
9.10ms |
2.09x |
In |
27.38ms |
85.23ms |
3.11x |
10000x |
FastWS |
RN |
|
Out |
27.49ms |
91.04ms |
3.31x |
In |
231.99ms |
854.76ms |
3.68x |
|
Large JSON payload - Text
Android |
iOS |
100x |
FastWS |
RN |
|
Out |
1.78ms |
3.70ms |
2.07x |
In |
3.36ms |
21.15ms |
6.30x |
1000x |
FastWS |
RN |
|
Out |
9.83ms |
21.67ms |
2.20x |
In |
18.00ms |
210.93ms |
11.72x |
10000x |
FastWS |
RN |
|
Out |
79.65ms |
218.06ms |
2.74x |
In |
251.55ms |
2179.68ms |
8.67x |
|
100x |
FastWS |
RN |
|
Out |
0.36ms |
0.85ms |
2.34x |
In |
4.38ms |
10.82ms |
2.47x |
1000x |
FastWS |
RN |
|
Out |
3.42ms |
6.03ms |
1.76x |
In |
28.66ms |
86.16ms |
3.01x |
10000x |
FastWS |
RN |
|
Out |
23.08ms |
57.33ms |
2.48x |
In |
265.89ms |
836.70ms |
3.15x |
|
Large JSON payload - Binary
Android |
iOS |
100x |
FastWS |
RN |
|
Out |
1.94ms |
11.36ms |
5.85x |
In |
2.64ms |
27.02ms |
10.23x |
1000x |
FastWS |
RN |
|
Out |
18.00ms |
105.60ms |
5.87x |
In |
23.72ms |
248.20ms |
10.47x |
10000x |
FastWS |
RN |
|
Out |
X |
X |
X |
In |
X |
X |
X |
|
100x |
FastWS |
RN |
|
Out |
0.50ms |
6.13ms |
12.29x |
In |
4.55ms |
14.47ms |
3.18x |
1000x |
FastWS |
RN |
|
Out |
4.27ms |
39.44ms |
9.24x |
In |
26.82ms |
104.71ms |
3.90x |
10000x |
FastWS |
RN |
|
Out |
29.12ms |
417.89ms |
14.35x |
In |
240.16ms |
1060.69ms |
4.42x |
|
Large Binary (Image - 234KB)
Android |
iOS |
|
FastWS |
RN |
|
Out |
x |
x |
x |
In |
x |
x |
x |
|
100x |
FastWS |
RN |
|
In |
11.71ms |
1770.53ms |
151.20x |
|
All tests were performed with local WebSocket server to minimize network variance. Each test was run enough times to have 10 samples, after removing outliers.
In the future, we would like to add benchmarks for:
- CPU usage during message processing
- Memory footprint during large transfers
- Connection establishment time
- Concurrent connection handling
- Message latency under high load
Your contributions are welcome! Check out our contributing guidelines to help improve these benchmarks.
Made with ❤️ at Callstack
React Native FastWS is an open source project and will always remain free to use. If you think it's cool, please star it 🌟. Callstack is a group of React and React Native geeks, contact us at [email protected] if you need any help with these or just want to say hi!
Like the project? ⚛️ Join the team who does amazing stuff for clients and drives React Native Open Source! 🔥