Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alexzhang1030/mche
WebRTC & WebSocket Message Channel Helpers
https://github.com/alexzhang1030/mche
typescript webrtc websocket
Last synced: 3 months ago
JSON representation
WebRTC & WebSocket Message Channel Helpers
- Host: GitHub
- URL: https://github.com/alexzhang1030/mche
- Owner: alexzhang1030
- License: mit
- Created: 2024-04-03T17:50:54.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2024-05-16T09:07:38.000Z (9 months ago)
- Last Synced: 2024-05-17T02:08:01.643Z (9 months ago)
- Topics: typescript, webrtc, websocket
- Language: TypeScript
- Homepage:
- Size: 215 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mche
**M**essage **C**hannel **He**lpers
This is a library that provides some utilities that helps you to communicate data between different devices(in browser).
It's simple if you want to build a collaboration application by using `mche`.
We provide first-class support to `WebRTC` and `WebSocket`.
> ⚠️ WARNING: this project is not stable yet, please use it carefully. Expect breaking changes.
## Installation
```bash
pnpm i mche
```## Options
You can choose `WebRTC` or `WebSocket` to communicate with other peers.
```ts
interface MCHelperOptionsBase {
/**
* The ID of the peer.
*/
id: string/**
* Whether to print debug information.
* - `'verbose'`: print all debug information
* - `true`: print connection information
*
* @default false
*/
debug?: boolean | 'verbose'/**
* Room id.
*
* Same room id will be able to communicate with each other.
*/
roomId: string
}interface MCHelperOptionsWebRTC extends MCHelperOptionsBase {
mode: 'webrtc'/**
* The URL of the signaling server or ws instance.
* @example "wss://example.com:8080"
*/
signalingServerUrlOrWsInstance: string | WebSocket/**
* ICE servers.
* @default
* [
* { urls: 'stun:stun.l.google.com:19302', }
* ]
*/
iceServers?: RTCIceServer[]
}interface MCHelperOptionsWebSocket extends MCHelperOptionsBase {
mode: 'websocket'/**
* The URL or ws instance.
* @example "wss://example.com:8080"
*/
urlOrWsInstance: string | WebSocket
}type MCHelperOptions = MCHelperOptionsWebRTC | MCHelperOptionsWebSocket
```## Usage
```ts
import { MCHelper } from 'mche'const mch = new MCHelper({
// ...options
})mche.onMessageChannelReady(() => {
// MessageChannel is ready...
})mch.broadcast('hello')
mch.onBroadcast((data) => {
console.log(data)
})// Call close if you want to close the connection.
mch.close()
```### Signaling Server
You can use this [signaling server example](https://github.com/alexzhang1030/mche-signaler). Or write your own signaling server.
#### Utilities
```ts
import { withHandleMetaEvent } from 'mche/server'const {
getHeartbeatResponse,
isHeartbeatRequestParsed,
getMetaCloseResponse,
getMetaRegisterAcceptResponse,
getMetaRegisterResponse,
getMetaRegisterEventPayload,
isMetaRegisterEvent,
tryParseMetaEvent,
} = withHandleMetaEvent()
```You can use these functions to implement your own signaling server.
But recommended to checkout the [signaling server example](https://github.com/alexzhang1030/mche-signaler)
## License
MIT