yjs / y-webrtc

WebRTC Connector for Yjs
MIT License
448 stars 109 forks source link

Silencing "Unable to compute message" #18

Closed maxkrieger closed 3 years ago

maxkrieger commented 3 years ago

In my app I'm using the simple-peer method peer.send(mymessage) to get around some of the overhead(?) involved in the yjs CRDT: i.e., to broadcast cursor position. However, because of the switch statement in this library, unknown types of messages throw an error:

https://github.com/yjs/y-webrtc/blob/d9f2b28b2ab28edfc11a0b0b48f3bc0a5919f8bf/src/y-webrtc.js#L117

Could you add a way to silence this error or otherwise allow custom messages to be handled?

dmonad commented 3 years ago

Mi Max,

What kind of overhead are you talking about?

Cursor positions should be propagated using the awareness protocol. https://github.com/yjs/y-protocols#awarenessprotocolawareness-class

In most cases, the Awareness instance should work better than implementing your own protocol. y-webrtc doesn't guarantee a totally connected network so you need some CRDT to propagate data. Otherwise, you will likely run into sync issues.

If you are interested we can discuss an extension to the y-webrtc protocol that handles custom messages. Some kind of "custom" message format.

maxkrieger commented 3 years ago

This was exactly what I needed, thanks so much!

Btw, for streams, I only have access to the peerId because of the way the simple-peer api works -- but I'd like to match that peerId with its corresponding ydoc id immediately so that I know whose stream is whose. I was thinking of attaching the room.peerId to each peer's awareness state as soon as it becomes available so that I can match up the proper stream just by querying the awareness states for the right peerId.

However the room is null until the client connects to the TURN(?) servers, so I can't access my own peerId until some inaccessible connect event. Right now I'm just waiting for the first event to come through to check the room object, but is there a more principled way of getting my peerId as soon as it's available?

dmonad commented 3 years ago

Hi @maxkrieger , you could wait for the key promise.

Before we can create a room, we need to compute the encryption key which is an asynchronous background call.

The proper way to wait for the room would be provider.key.then(() => { provider.room != null })

maxkrieger commented 3 years ago

Thanks so much!