Skip to content

Commit

Permalink
fix: merge pull request #38 from ivelin/master
Browse files Browse the repository at this point in the history
build: gitpod ready
  • Loading branch information
Ivelin Ivanov committed Nov 28, 2020
2 parents 6ad4239 + 6b68cfa commit 12a4365
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
tasks:
- init: 'echo "TODO: Replace with init/build command"'
command: 'echo "TODO: Replace with command to start project"'
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![Gitpod ready-to-code](https://img.shields.io/badge/Gitpod-ready--to--code-blue?logo=gitpod)](https://gitpod.io/#https://github.com/ambianic/peerjs-python)

# peerjs-python

Python port of [PeerJS](https://github.com/peers) client.
Expand Down Expand Up @@ -60,6 +62,55 @@ Initial working prototype completed. PeerJS Python is now able to connect over W
- [ ] >90% code coverage with CI tests.
- [ ] Port media support.

## Code Examples

A typical p2p session takes these steps:
1. Establish signaling server session that enables peers to discover each other.
2. Discover remote peer ID (either via signaling server room affinity or other means)
3. Request connection to remote peer via signaling server
4. Connect to remote peer via WebRTC ICE protocol.
5. Exchange data or media with remote peer over p2p WebRTC connection.

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](https://github.com/ambianic/peerjs-python/blob/master/src/peerjs/ext/http-proxy.py).



## Other Related Open Source projects

There are several great projects that solve the problem of accessing IoT devices behind firewall via tunneling servers.
Expand Down
3 changes: 0 additions & 3 deletions src/peerjs/ext/http-proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,6 @@ async def peer_disconnected(peerId):
'Resetting to last known ID.')
peer._id = savedPeerId
peer._lastServerId = savedPeerId
global _is_shutting_down
if not _is_shutting_down:
await peer.reconnect()

@peer.on(PeerEventType.Close)
def peer_close():
Expand Down

0 comments on commit 12a4365

Please sign in to comment.