Python port of PeerJS client
Python port of PeerJS client.
Enables Progressive Web Apps to discover and pair directly with Python apps using secure, browser supported WebRTC protocol.
See Ambianic UI PNP module for a real-world example how PeerJS Python is used with PnP and HTTP Proxy.
Uses aiortc as Python WebRTC provider. This requires installing a few native dependencies for audio/video media processing.
On Debian/Ubuntu run:
apt install libavdevice-dev libavfilter-dev libopus-dev libvpx-dev pkg-config libsrtp2-dev
On OS X run:
brew install ffmpeg opus libvpx pkg-config
This project was originally motivated while searching for a way to:
This article in WebRTCHacks provides more insight into the background of this project.
Initial working prototype completed. PeerJS Python is now able to connect over WebRTC DataChannel to PeerJS in the browser and exchange messages.
A typical p2p session takes these steps:
The following code snippet shows the initial part of establishing a signaling server connection.
options = PeerOptions(
host=config['host'],
port=config['port'],
secure=config['secure'],
token=new_token,
config=RTCConfiguration(
iceServers=[RTCIceServer(**srv) for srv in config['ice_servers']]
)
)
peer = Peer(id=savedPeerId, peer_options=options)
await peer.start()
log.info('peer activated')
_setPnPServiceConnectionHandlers(peer)
Once a signaling server connection is established, a peer can request connection to another peer or listen for requests from a remote peer. The example snippet bellow shows the latter:
@peer.on(PeerEventType.Connection)
async def peer_connection(peerConnection):
log.info('Remote peer trying to establish connection')
_setPeerConnectionHandlers(peerConnection)
After a p2p connection is established, a peer can receive and send application messages. The following snippet shows how a peer receives a message:
@peerConnection.on(ConnectionEventType.Data)
async def pc_data(data):
log.debug('data received from remote peer \n%r', data)
For a complete working example see this file.
There are several great projects that solve the problem of accessing IoT devices behind firewall via tunneling servers.
A few popular p2p projects that use WebRTC: