0.9.0 • Published 14 days ago

mche v0.9.0

Weekly downloads
-
License
MIT
Repository
github
Last release
14 days ago

mche

Message Channel Helpers

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

pnpm i mche

Options

You can choose WebRTC or WebSocket to communicate with other peers.

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

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. Or write your own signaling server.

Utilities

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

License

MIT

0.9.0

14 days ago

0.8.8

17 days ago

0.8.5

17 days ago

0.8.4

17 days ago

0.8.7

17 days ago

0.8.6

17 days ago

0.8.0-beta.3

17 days ago

0.8.0-beta.4

17 days ago

0.8.0-beta.1

17 days ago

0.8.0-beta.2

17 days ago

0.8.1

17 days ago

0.8.0

17 days ago

0.8.3

17 days ago

0.8.2

17 days ago

0.6.2

1 month ago

0.7.0

23 days ago

0.6.1

1 month ago

0.6.0

1 month ago

0.5.4

1 month ago

0.5.3

1 month ago

0.5.2

1 month ago

0.5.1

1 month ago

0.4.8

1 month ago

0.4.7

1 month ago

0.5.0

1 month ago

0.4.5

1 month ago

0.4.4

1 month ago

0.4.6

1 month ago

0.4.1

1 month ago

0.4.0

1 month ago

0.4.3

1 month ago

0.4.2

1 month ago

0.3.0

2 months ago

0.2.0

2 months ago

0.1.1

2 months ago

0.3.2

2 months ago

0.3.1

2 months ago

0.3.3

2 months ago

0.1.0

2 months ago

0.0.11

2 months ago

0.0.10

2 months ago

0.0.9

2 months ago

0.0.8

2 months ago

0.0.7

2 months ago

0.0.6

2 months ago

0.0.5

2 months ago

0.0.4

2 months ago

0.0.3

2 months ago

0.0.2

2 months ago

0.0.1

2 months ago

0.0.0

2 months ago