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
.
⚠️ WARNING: this project is not stable yet, please use it carefully. Expect breaking changes.
pnpm i mche
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()
You can use this signaling server example. Or write your own signaling server.
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.
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.
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>" }
}
}
MIT