mche
TypeScript icon, indicating that this package has built-in type declarations

0.6.2 • Public • Published

mche

Message Channel Helpers

NPM Version NPM Downloads License

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.

⚠️ WARNING: this project is not stable yet, please use it carefully. Expect breaking changes.

Installation

pnpm i mche

Usage

import { MCHelper } from 'mche'

const mch = new MCHelper({
  signalingServer: 'ws://localhost:8080',
  id: '<uniqueSelfId>',
  roomId: '<uniqueRoomId>',
})

mch.broadcast('hello')

mch.onBroadcast((data) => {
  console.log(data)
})

// Call close if someone is leaving the room
mch.close()

Signaling Server

You can use this signaling server example. Or write your own signaling server.

Receive Register

Once a new user joins the room, the signaling server will receive a message like this:

{
  "event": "register",
  "data": {
    "roomId": "<roomId>",
    "id": "<id>"
  }
}

You need to broadcast join to all users in the same room.

Broadcast Join

You need broadcast the room users to new user when they join the room. The data structure should be like this:

{
  "event": "open",
  // The peer users ids
  "data": {
    "roomId": "<roomId>",
    "data": [
      { "id": "<id1>", "userData": { } },
      { "id": "<id2>", "userData": { } }
    ]
  }
}

We actually connect other peers by using WebRTC after we get the open event. So ids should be required for connecting.

Broadcast Leave

You need broadcast the room users when someone leave the room. The data structure should be like this:

{
  "event": "close",
  // The leaved user id
  "data": {
    "roomId": "<roomId>",
    "data": { "id": "<id>" }
  }
}

License

MIT

Package Sidebar

Install

npm i mche

Weekly Downloads

206

Version

0.6.2

License

MIT

Unpacked Size

38.6 kB

Total Files

7

Last publish

Collaborators

  • alexzzz