Ultra-high-performance networking to React Native by combining a memory-safe Rust core with the zero-overhead Nitro Modules JSI bridge. Provides Node.js-compatible net, tls, http(s) API.
npm install react-native-nitro-netUltra-high-performance networking to React Native by combining a memory-safe Rust core with the zero-overhead Nitro Modules JSI bridge. Provides Node.js-compatible net, tls, http(s) API.
![license]()
![platform]()
![compatibility]()
δΈζζζ‘£
* π High Performance: Built on top of Rust's tokio asynchronous runtime.
* π€ Node.js Compatible: Implements standard net, tls, http, and https APIs.
* π‘οΈ Modern Security: TLS implementation powered by Rustls 0.23 (Ring provider), supporting TLS 1.2 and 1.3.
* π Full Protocol Support: Support for PEM/PFX certificates, SNI, HTTP Trailers, 100 Continue, Protocol Upgrades (101), and HTTP Tunneling (CONNECT).
* β‘ Nitro Modules: Uses JSI for zero-overhead communication between JavaScript and Native code.
* π‘οΈ Robust & Stable: Advanced fixes for port reuse, deadlocks, and connection pooling hangs.
* π± Cross-Platform: Supports both iOS and Android.
``bash`
npm install react-native-nitro-netor
yarn add react-native-nitro-net
Requires pod install to link the native libraries.
`bash`
cd ios && pod install
This library uses a high-performance three-layer architecture:
1. JavaScript Layer: Provides high-level Node.js compatible net and tls APIs using readable-stream and EventEmitter.
2. C++ Bridge (Nitro): Handles the zero-copy orchestration between JS and Rust using Nitro Hybrid Objects and JSI.
3. Rust Core: Implements the actual networking logic using the Tokio asynchronous runtime, providing memory safety and high concurrency.
`typescript
import net from 'react-native-nitro-net';
const client = net.createConnection({ port: 8080, host: '1.1.1.1' }, () => {
console.log('Connected!');
client.write('Hello Server!');
});
client.on('data', (data) => {
console.log('Received:', data.toString());
});
client.on('error', (err) => {
console.error('Error:', err.message);
});
`
The server supports binding to a dynamic port by using 0.
`typescript
import net from 'react-native-nitro-net';
const server = net.createServer((socket) => {
socket.write('Echo: ' + socket.read());
});
// Use 0 for dynamic port allocation
server.listen(0, '127.0.0.1', () => {
const address = server.address();
console.log(Server listening on dynamic port: ${address?.port});`
});
`typescript
import { tls } from 'react-native-nitro-net';
// Client connection
const socket = tls.connect({
host: 'example.com',
port: 443,
servername: 'example.com', // SNI
}, () => {
console.log('Securely connected!');
console.log('Protocol:', socket.getProtocol());
});
// Server with PFX
const server = tls.createServer({
pfx: fs.readFileSync('server.pfx'),
passphrase: 'your-password'
}, (socket) => {
socket.write('Secure hello!');
});
server.listen(443);
`
* Advanced Features: Supports keylog event re-emission for Wireshark, session resumption, and asyncDispose.headersTimeout
* Performance Tuning: Configurable , keepAliveTimeout, and requestTimeout.
* Resource Management: Strict protective shutdown logic in Rust to prevent socket and Unix domain socket file leaks.
Implementation of the standard Node.js http API.
`typescript
import { http } from 'react-native-nitro-net';
http.get('http://google.com', (res) => {
console.log(Status: ${res.statusCode});Body segment: ${chunk.length} bytes
res.on('data', (chunk) => console.log());`
res.on('end', () => console.log('Request complete'));
});
Uses https and the built-in Agent for connection reuse.
`typescript
import { https } from 'react-native-nitro-net';
const agent = new https.Agent({ keepAlive: true });
https.get('https://api.github.com/users/margelo', { agent }, (res) => {
// ... handle response
});
`
`typescript
import net from 'react-native-nitro-net';
const client = net.createConnection({ port: 8080, host: '127.0.0.1' }, () => {
client.write('Hello Server!');
});
`
The server supports binding to a dynamic port by using 0.
`typescript
import net from 'react-native-nitro-net';
const server = net.createServer((socket) => {
socket.write('Echo: ' + socket.read());
});
server.listen(0, '127.0.0.1', () => {
const address = server.address();
console.log(Server listening on dynamic port: ${address?.port});`
});
> [!IMPORTANT]
> Server.close() Behavior: Unlike Node.js's default behavior where server.close() only stops accepting new connections, this implementation immediately destroys all active connections when close() is called. This ensures clean resource release and is more intuitive for mobile applications.
| Property / Method | Description |
| --- | --- |
| connect(options) | Connect to a remote host/port or Unix path. |write(data)
| | Send data asynchronously. Supports backpressure. |destroy()
| | Immediate closing of the socket and resource cleanup. |setNoDelay(bool)
| | Control Nagle's algorithm. |setKeepAlive(bool)
| | Enable/disable keep-alive. |address()
| | Returns { port, family, address } for the local side. |
Events: connect, ready, data, error, close, timeout, lookup.
| Property / Method | Description |
| --- | --- |
| authorized | true if peer certificate is verified. |getProtocol()
| | Returns negotiated TLS version (e.g., "TLSv1.3"). |getCipher()
| | Returns current cipher information. |getPeerCertificate()
| | Returns detailed JSON of the peer certificate. |getSession()
| | Returns the session ticket for resumption. |encrypted
| | Always true. |
Events: secureConnect, session, keylog, OCSPResponse.
| Method | Description |
| --- | --- |
| initWithConfig(options) | Optional. Initializes the Rust runtime with custom settings (e.g., workerThreads, debug). Must be called before any other operation. |setVerbose(bool)
| | Toggle detailed logging for JS, C++, and Rust. |isIP(string)
| | Returns 0, 4, or 6. |
| Method | Description |
| --- | --- |
| listen(options) | Start listening. Supports port: 0 for dynamic allocation. |close()
| | Stops the server and destroys all active connections. |address()
| | Returns the bound address (crucial for dynamic ports). |getConnections(cb)
| | Get count of active connections. |renegotiate(opt, cb)
| | Shim: Returns ERR_TLS_RENEGOTIATION_DISABLED (Rustls security policy). |
Events: listening, connection, error, close, connect (HTTP Tunneling).
Supported methods: listen, close, addContext, setTicketKeys, getTicketKeys.secureConnection
Events: , keylog, newSession.
Enable verbose logging to see the internal data flow across JS, C++, and Rust:
`typescript
import { setVerbose } from 'react-native-nitro-net';
setVerbose(true);
`
Logs will be visible in your native debugger (Xcode/logcat) and JS console, prefixed with [NET DEBUG] or [NET NATIVE]`.
ISC