rtc-link
Version:
A library for managing WebRTC connections with multiplexed streams, anti-glare handling, and streamlined data channel setup.
102 lines (62 loc) • 3.88 kB
Markdown
# rtc-link
A lightweight library for managing WebRTC connections with multiplexed streams, anti-glare handling, and streamlined data channel setup. Useful for setting up signaling, handling offers/answers, and multiplexing over WebRTC data channels.
# `IN BETA TESTING PHASE`
## Installation
Install via npm:
```bash
npm install rtc-link
```
---
### Important Notice: WebRTC Testing Segmentation Fault
🚨 **Known Issue**: Tests involving WebRTC functionality currently result in a segmentation fault (exit code 139) due to an issue in the `@roamhq/wrtc` library. This affects the `establishSignalStream` and `establishWrtcStream` functions in the Node.js testing environment, causing segmentation faults.
**Browser Compatibility**: This issue only impacts Node.js testing environments using `@roamhq/wrtc`. In the browser, we use the native WebRTC APIs, which do not encounter this issue and perform as expected.
The maintainers of `@roamhq/wrtc` are actively working on a fix. Until then, WebRTC-related tests in Node.js may not pass successfully.
**Workaround**: If your work involves other aspects of the project, you can comment out or skip WebRTC-related tests temporarily.
Thank you for your patience and understanding.
---
## Functions
### `establishWrtcStream`
Creates a WebRTC data channel stream, establishing a peer-to-peer connection with support for multiplexing and anti-glare protection to prevent conflicts if both peers try to initiate.
#### Usage
```javascript
import { establishWrtcStream } from 'rtc-link';
const isInitiator = true;
const wrtcStream = establishWrtcStream(isInitiator, stream, { iceServers: [{ urls: 'stun:stun.l.google.com:19302' }] });
```
#### Parameters
- `isInitiator` (boolean): Indicates if this peer initiates the connection.
- `stream` (Stream): The transport stream used for WebRTC signaling.
- `config` (Object, optional): Configuration for the WebRTC connection.
- `iceServers` (Array): ICE servers for NAT traversal, defaulting to Google's public STUN server.
#### Description
`establishWrtcStream` configures a WebRTC connection over a given transport stream. The function initiates a data channel for duplex communication and handles offers, answers, and ICE candidate exchanges. Anti-glare protection ensures smooth connection even if both peers attempt to initiate simultaneously. It also regenerates the connection on errors, maintaining a stable communication link.
#### Example
```javascript
const wrtcStream = establishWrtcStream(true, myStream, {
iceServers: [{ urls: 'stun:stun.l.google.com:19302' }]
});
```
### `establishSignalStream`
Establishes a signaling connection between two streams, handling WebRTC signaling messages (offer, answer, and ICE candidates) between the peers.
#### Usage
```javascript
import { establishSignalStream } from 'rtc-link';
establishSignalStream(stream1, stream2);
```
#### Parameters
- `stream1` (Stream): The first data stream.
- `stream2` (Stream): The second data stream.
#### Description
`establishSignalStream` creates a bi-directional signaling pipeline between two data streams using multiplexing. It listens for WebRTC messages on designated channels and forwards each message type to the corresponding channel on the target stream. Additionally, this function closes signaling streams if either stream closes, ensuring reliable connection management.
- **WebRTC Channels**:
- `CHANNEL.WEBRTC_OFFER`: Handles WebRTC offer messages.
- `CHANNEL.WEBRTC_ANSWER`: Handles WebRTC answer messages.
- `CHANNEL.WEBRTC_ICE`: Handles ICE candidate messages.
#### Example
```javascript
establishSignalStream(stream1, stream2);
```
## Error Handling
Both functions emit any connection errors through the returned data channel, making it easy to track and manage issues.
## License
This project is licensed under the MIT License.