Documentation
¶
Overview ¶
Package webrtc carries gRPC between two processes that cannot reach each other.
What it is for ¶
gRPC assumes one side can listen and the other can dial. Two peers behind household routers can do neither: neither has an address the other can connect to, and there is nothing in between that either controls. WebRTC is how the web solved that — each side describes itself, the two swap those descriptions by any means at all, and what results is a direct connection.
This presents that connection to gRPC as what gRPC expects: a net.Conn on one side and a net.Listener on the other. Everything above it is ordinary gRPC — unary calls, streaming, interceptors, deadlines — because nothing above it knows what it is running on.
What it does not do ¶
It does not do the signalling. Two peers have to swap a session description before there is anything to carry gRPC over, and how they do that is not a transport's business: a mail, a chat window, a QR code, a rendezvous service somebody already runs. A library that chose one would be choosing for every caller, and the choice is usually already made by whatever the two peers are doing together.
So this takes a peer connection that is already established. What that costs a caller is twenty lines of pion; what it buys is that this package has no opinion about how two people find each other.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrClosed = errors.New("webrtc: listener closed")
ErrClosed is returned by a listener that has been closed.
var ErrTransport = errors.New("webrtc: transport")
ErrTransport reports a data channel that could not be turned into a connection.
Functions ¶
func Conn ¶
func Conn(dc *pion.DataChannel) (net.Conn, error)
Conn presents an established data channel as a net.Conn, which is what gRPC wants of a transport and all it wants.
The channel is detached from pion's callback delivery first: a gRPC transport reads, and a channel that hands its data to a callback cannot be read from. It must therefore have been created on a peer connection configured with SettingEngine.DetachDataChannels, which is what pion requires and what the examples in this package do.
func DialOption ¶
func DialOption(dc *pion.DataChannel) (grpc.DialOption, error)
DialOption returns a grpc.DialOption that carries every gRPC channel over an established data channel.
Combine it with insecure transport credentials. That is not a weakening: WebRTC is encrypted end to end by DTLS and cannot be otherwise — there is no unencrypted mode to fall back to — so a second layer of TLS inside it would be encrypting what is already encrypted, against an attacker who is not there. It is the same reasoning as wss:// in the WebSocket transport beside this one.
The address a caller passes to grpc.NewClient is ignored, because there is no address: the connection already exists and there is nothing to resolve. Pass "passthrough:///webrtc" so that gRPC does not try.
func Listen ¶
func Listen(pc *pion.PeerConnection) net.Listener
Listen presents a peer connection as a net.Listener, so that a standard grpc.Server can Serve it.
Every data channel the peer opens becomes one connection. That is more than gRPC needs — it multiplexes its own streams over one — but it costs nothing to allow and it is what makes a second gRPC server, or a second client, possible over the same peer connection without a second negotiation.
The peer connection must have been created with a setting engine that detaches data channels; a channel delivering to a callback cannot be read from, and pion says so at detach time rather than here.
Closing the listener stops accepting. It does not close the peer connection, which the caller made and may still be using — for a video call, for instance, which is often exactly what two peers doing this are already on.
Types ¶
This section is empty.
