y-webrtc icon indicating copy to clipboard operation
y-webrtc copied to clipboard

Allow custom signalling classes

Open Tails opened this issue 4 years ago • 2 comments

I have a websocket connection that expects a JSON-RPC envelope (Holochain). To support this I need to wrap the message that is sent over the websocket in the SignalingConn class. It would be great if the class would be defined as a constructor argument so it would not be needed to override the WebrtcProvider.connect(). Alternatively the SignalingConn class could be returned by a getSignalingConnectionClass() hook that can be overrided more easily.

https://github.com/yjs/y-webrtc/blob/5fffe6b74bfbf3e53449dd193312533f20afa367/src/y-webrtc.js#L595

Tails avatar May 18 '20 08:05 Tails

I'm not a big fan of constructor magic. I wouldn't be able to guarantee that your custom signaling conn is properly managed.

I believe in this case the right approach is to extend the connect method.

// Your custom signaling conns should be maintained in a separate map:
const holoSignalingConns = new Map()

class HoloWebrtcProvider {
  connect () {
    super.connect()
    const holoSignalingConn = map.setIfUndefined(holoSignalingConns, 'your custom endpoint definition', () => new HoloSignalingConn())
      this.signalingConns.push(holoSignalingConn)
  }
}

This would also allow you to use HoloSignalingConn and pure SignalingConn in combination.

dmonad avatar May 18 '20 11:05 dmonad

@dmonad we have been trying to do something similar and ran into some issues. We created #62 as a possible refactor to allow us to swap in a different signaling server implementation. Would you be willing to review that approach and provide feedback and whether this is something that could be merged into y-webrtc?

byrond avatar Jan 02 '24 15:01 byrond