Skip to content

Commit 10bf9e1

Browse files
committed
chore: readme and documentation (#7)
* chore: add table * update * chore: update readme * chore: add meta stuff * chore: Delete gem files * chore: reorganise files * chore: updates * chore: up * chore: add title * up * chore: use div * chore * chore: tweaks * chore: update readme
1 parent 9640bd6 commit 10bf9e1

File tree

10 files changed

+219
-163
lines changed

10 files changed

+219
-163
lines changed

CONTRIBUTING.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Contributing
2+
3+
We welcome contributions! Whether it's bug fixes, feature improvements, or documentation updates, your help is appreciated.
4+
5+
This project follows the [Contributor Covenant](https://www.contributor-covenant.org/version/2/1/code_of_conduct/) Code of Conduct. By participating, you are expected to uphold this code.
6+
7+
## Commit Messages
8+
9+
We use [Conventional Commits](https://www.conventionalcommits.org/) for clear communication and automated versioning. Please format your commit messages as follows:
10+
11+
```
12+
type(scope): description
13+
14+
[optional body]
15+
[optional footer]
16+
```
17+
18+
Where `type` is one of:
19+
- `feat:` New features
20+
- `fix:` Bug fixes
21+
- `docs:` Documentation changes
22+
- `chore:` Maintenance tasks
23+
- `test:` Adding or updating tests
24+
- `refactor:` Code changes that neither fix bugs nor add features
25+
26+
## Development
27+
28+
This library is built with [Nitro](https://mrousavy.github.io/nitro). After making changes to native code, you must run:
29+
30+
```bash
31+
npm run codegen
32+
```
33+
34+
This regenerates the necessary bindings between JavaScript and native code.
35+
36+
## Working on the Example App
37+
38+
Install dependencies:
39+
```bash
40+
npm install
41+
```
42+
43+
Then, follow these steps to run the app on iOS:
44+
```bash
45+
cd ./example && npm run ios
46+
```
47+
48+
or on Android:
49+
```bash
50+
cd ./example && npm run android
51+
```
52+
53+
You can also open the example project directly in Xcode (`example/ios/NitroPlayground.xcworkspace`) or Android Studio (`example/android`) for native development.
54+
55+
Make sure to run the WebSocket server in the background:
56+
```bash
57+
cd ./example && bun server.ts
58+
```
59+
60+
Otherwise, the app will not be able to connect to the server and will fail with a connection error.

Gemfile

Lines changed: 0 additions & 8 deletions
This file was deleted.

Gemfile.lock

Lines changed: 0 additions & 105 deletions
This file was deleted.

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Callstack
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 80 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
`react-native-fast-ws`
2-
======
1+
<div align="center">
2+
<h1>react-native-fast-ws</h1>
3+
</div>
34

4-
Blazing fast WebSocket client for React Native built on top of [Nitro](https://mrousavy.github.io/nitro).
5+
<p align="center">
6+
<img src="https://media1.tenor.com/m/7Ienx0j5cqoAAAAC/fast-and.gif" />
7+
</p>
58

6-
> [!WARNING]
7-
> 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.
9+
<p align="center">
10+
When your WebSockets hit the Nitro boost 🏎️💨
11+
</p>
12+
13+
14+
---
815

916
### Getting started
1017

@@ -15,54 +22,88 @@ npm install react-native-fast-ws --save
1522
> [!NOTE]
1623
> This package requires React Native 0.76 and New Architecture. You must also install and configure [Nitro Modules](https://github.com/mrousavy/nitro) to use this package.
1724
18-
### TODOs
25+
> [!WARNING]
26+
> 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.
27+
28+
### Usage
29+
30+
This library implements the [WebSocket Web API](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) and is meant to be a drop-in replacement for React Native's WebSocket implementation.
1931

20-
- Document things not implemented in spec, or different from spec
21-
- Fix outstanding (and known) issues
22-
- Figure out a better name and logo
23-
- Stable release at [React Native London Conf](https://www.reactnativelondon.co.uk)
24-
- Extract Blob to a separate package
25-
- Documentation
32+
```ts
33+
// Create a new WebSocket instance
34+
const ws = new WebSocket('wss://echo.websocket.org');
2635

27-
If you want to contribute, please reach out (you can find me on [X/Twitter](https://x.com/grabbou)).
36+
// Using event listeners (recommended)
37+
ws.addEventListener('open', (event) => {
38+
console.log('Connected to server');
39+
ws.send('Hello Server!');
40+
});
41+
42+
// Or using event handlers
43+
ws.onopen = (event) => {
44+
console.log('Connected to server');
45+
};
46+
47+
// Listen to messages
48+
ws.addEventListener('message', (event) => {
49+
console.log('Received:', event.data);
50+
});
51+
52+
// Send different types of data
53+
ws.send('Plain text message');
54+
ws.send(new Blob(['Binary data']));
55+
ws.send(new Uint8Array([1, 2, 3, 4]));
56+
57+
// Close the connection when done
58+
ws.close();
59+
```
60+
61+
For more detailed information about the WebSocket API, check out the [MDN WebSocket documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket).
2862

2963
### Differences with React Native
3064

31-
**Blob**
32-
- Can create `Blob` from `ArrayBuffer`, `ArrayBufferView`, `Blob`, or `string`. With React Native, [you can only create Blob from string or another Blob](https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Blob/BlobManager.js#L69-L73)
33-
- Has `text()`, `arrayBuffer()`, and `stream()` methods. With React Native, you can only access the contents of `Blob` via [custom `data` field](https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Blob/Blob.js#L75-L82)
34-
- Does not depend on a native module. Uses `ArrayBuffer`s natively, thanks to Nitro Modules. With React Native, [Blob uses `RCTBlobManager` under the hood](https://github.com/facebook/react-native/blob/bd323929dc5be5666ee36043babec7d981a095dc/packages/react-native/Libraries/Blob/RCTBlobManager.h#L15) to create and hold references to the data
35-
- Automatically deallocated when no longer in use. [You don't have to `close()` it manually](https://github.com/facebook/react-native/blob/bd323929dc5be5666ee36043babec7d981a095dc/packages/react-native/Libraries/Blob/Blob.js#L122-L138)
65+
- Can create `Blob`s from `ArrayBuffer`, `ArrayBufferView` too. With React Native, [you can only create Blob from string or another Blob](https://github.com/facebook/react-native/blob/af384a914a4e9ef6a5d25b00bc14b0483e5af879/packages/react-native/Libraries/Blob/BlobManager.js#L69-L73)
66+
- Each `Blob` has `text()`, `arrayBuffer()`, and `stream()` methods. With React Native, you can only access the contents of `Blob` via [custom `data` field](https://github.com/facebook/react-native/blob/af384a914a4e9ef6a5d25b00bc14b0483e5af879/packages/react-native/Libraries/Blob/Blob.js#L75-L82)
67+
- `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](https://github.com/facebook/react-native/blob/bd323929dc5be5666ee36043babec7d981a095dc/packages/react-native/Libraries/Blob/RCTBlobManager.h#L15) to create and hold references to the data.
68+
- `Blob` is automatically deallocated when no longer in use. [You don't have to `close()` it manually](https://github.com/facebook/react-native/blob/bd323929dc5be5666ee36043babec7d981a095dc/packages/react-native/Libraries/Blob/Blob.js#L122-L138)
69+
- You can send `Blob`, `ArrayBuffer`, and `ArrayBufferView` objects directly. [With React Native, they are serialized and sent as base64 strings](https://github.com/facebook/react-native/blob/af384a914a4e9ef6a5d25b00bc14b0483e5af879/packages/react-native/Libraries/WebSocket/WebSocket.js#L201-L203)
70+
- Uses direct callbacks per WebSocket instance. [React Native broadcasts all events to JS and filters by connection ID, which is less efficient](https://github.com/facebook/react-native/blob/af384a914a4e9ef6a5d25b00bc14b0483e5af879/packages/react-native/React/CoreModules/RCTWebSocketModule.mm#L144-L157)
3671

37-
**Websockets**
38-
- Emits events (`open`, `message`, `error`, `close`) that extend from [`Event`](https://dom.spec.whatwg.org/#event)
39-
- On iOS, uses built-in APIs for WebSockets (available on iOS 13+). React Native uses `SocketRocket` instead
40-
- Uses callbacks instead of `RCTEventEmitter` to communicate new messages back to the JavaScript
72+
### Benchmarks
4173

42-
### Compatibility table
74+
> [!NOTE]
75+
> Benchmarks shown below operate in controlled environments and may not accurately reflect real-world performance. Results can vary significantly based on factors such as:
76+
> - Device model and OS version
77+
> - Network conditions and latency
78+
> - Message payload size and type
79+
> - Application state and concurrent operations
4380
44-
The table below compares the features and APIs available in react-native-fast-ws against the [WebSocket specification](https://websockets.spec.whatwg.org/) and React Native's built-in WebSocket implementation. A ✅ indicates full support, ⚠️ indicates partial support with limitations, and ❌ indicates no support.
81+
These benchmarks are provided to demonstrate theoretical performance benefits and should be used as a general reference rather than absolute performance indicators.
4582

46-
#### WebSocket
83+
#### iOS (iPhone 16 Pro, iOS 18.0)
4784

48-
| Feature | Specification | FastWS | React Native |
49-
|---------|--------------|-----------------------|------------------------|
85+
| Operation | React Native | FastWS | Improvement |
86+
|-----------|--------------|--------|-------------|
87+
| Send 10k strings | x | x | x |
88+
| Receive 10k strings | x | x | x |
89+
| Send 10k ArrayBuffers | x | x | x |
90+
| Receive 10k ArrayBuffers | x | x | x |
5091

51-
#### Blob
92+
#### Android (Medium Phone, Android 15)
5293

53-
| Feature | Specification | FastWS | React Native |
54-
|---------|--------------|-----------------------|------------------------|
94+
| Operation | React Native | FastWS | Improvement |
95+
|-----------|--------------|--------|-------------|
96+
| Send 10k strings | x | x | x |
97+
| Receive 10k strings | x | x | x |
98+
| Send 10k ArrayBuffers | x | x | x |
99+
| Receive 10k ArrayBuffers | x | x | x |
55100

56-
### Developing
101+
All tests were performed with local WebSocket server to minimize network variance. Each test was run 5 times and averaged.
57102

58-
Run the example app to test changes:
103+
In the future, we would like to add benchmarks for CPU and memory usage, since that's where FastWS should shine. Your contributions are welcome!
59104

60-
```bash
61-
// Start the demo server
62-
bun server.ts
105+
## Made with ❤️ at Callstack
63106

64-
// Run the app
65-
cd example && npm run ios
66-
```
107+
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](https://callstack.com) is a group of React and React Native geeks, contact us at [[email protected]](mailto:[email protected]) if you need any help with these or just want to say hi!
67108

68-
You can also start the app from Xcode, in case you want to make changes to the native code. In that case, open `example/ios/NitroPlayground.xcworkspace` and hit run.
109+
Like the project? ⚛️ [Join the team](https://callstack.com/careers/?utm_campaign=Senior_RN&utm_source=github&utm_medium=readme) who does amazing stuff for clients and drives React Native Open Source! 🔥

example/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"scripts": {
55
"android": "react-native run-android",
66
"ios": "react-native run-ios",
7-
"start": "react-native start"
7+
"start": "react-native start",
8+
"server": "bun server.ts"
89
},
910
"dependencies": {
1011
"@bacons/text-decoder": "^0.0.0",

server.ts renamed to example/server.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/// <reference types="bun-types" />
2+
3+
/**
4+
* This file is a simple WebSocket server that sends back messages to the client.
5+
* It is meant to be run directly with Bun.
6+
*/
7+
18
Bun.serve({
29
fetch(req, server) {
310
// upgrade the request to a WebSocket

0 commit comments

Comments
 (0)