Skip to main content

A Python implementation of PeerJS

Project description

peerjs-py

peerjs-py is a Python implementation of the PeerJS library, allowing peer-to-peer connections in Python applications.

Installation

To install peerjs-py, use pip:

pip install peerjs-py

Usage

Connecting to a PeerJS Server

First, import the necessary modules and create a Peer instance:

python
from peerjs_python import Peer, PeerOptions

Create a Peer instance

peer = Peer(PeerOptions(
host='your-peerjs-server.com',
port=9000,
secure=True # Use this if your server uses HTTPS
))

Listen for the 'open' event to know when the connection to the server is established

@peer.on('open')
def on_open(id):
print(f"My peer ID is: {id}")

Connecting to Another Peer

To connect to another peer and send text data:

#Establish a connection
conn = peer.connect('remote-peer-id')

@conn.on('open')
def on_open():
  # Connection is now open and ready for use
  conn.send('Hello, remote peer!')
@conn.on('data')
def on_data(data):
  print(f"Received: {data}")

Voice/Video Calls

For voice or video calls, you'll need to use additional libraries like PyAudio for audio processing. Here's a basic example:

python
import pyaudio
# Initialize PyAudio
p = pyaudio.PyAudio()
# Open a call connection

call = peer.call('remote-peer-id', {'audio': True, 'video': False})

@call.on('stream')
def on_stream(stream):
  # Handle the incoming audio stream
  # This is a simplified example and would need more code to actually play the audio

@peer.on('call')
def on_call(call):
  # Answer incoming calls automatically
  call.answer({'audio': True, 'video': False})

@call.on('stream')
def on_stream(stream):
  # Handle the incoming audio stream

File Transfer

To send files between peers:

#Sending a file
with open('file.txt', 'rb') as file:
  data = file.read()
  conn.send({'file': data, 'filename': 'file.txt'})

# Receiving a file
@conn.on('data')
def on_data(data):
  if isinstance(data, dict) and 'file' in data:
     with open(data['filename'], 'wb') as file:
       file.write(data['file'])
    print(f"Received file: {data['filename']}")

Error Handling

Always implement error handling to manage potential connection issues:


@peer.on('error')
def on_error(error):
  print(f"An error occurred: {error}")

Testing

To ensure the reliability and functionality of peerjs-py, we have implemented a comprehensive testing suite. Before running the tests, please note that you must have your own PeerJS signaling server set up.

Running Tests

We use pytest as our testing framework. To run the tests, follow these steps:

  1. Ensure you have pytest installed:

    pip install pytest
    
  2. Navigate to the project root directory.

  3. Run the tests using pytest:

    pytest
    

End-to-End Tests

We have two types of end-to-end (e2e) tests to verify the functionality of peerjs-py in different scenarios:

  1. Python Client to Python Client Test This test checks the communication between two Python clients using peerjs-py.

    To run this test:

    ./e2e/run-e2e.sh
    
  2. PeerJS Browser Client to Python Client Test This test verifies the compatibility between a PeerJS browser client and a Python client using peerjs-py.

    To run this test:

    ./e2e/run-e2e-test-py.sh
    

Important Notes

  • Ensure your PeerJS signaling server is running and accessible before executing the tests.
  • The end-to-end tests may require additional setup or dependencies. Please refer to the respective script files for any specific instructions.
  • If you encounter any issues during testing, check your server configuration and network connectivity.

By running these tests, you can verify that peerjs-py is working correctly in various scenarios and is compatible with both Python-to-Python and Browser-to-Python communications.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

peerjs_py-0.1.0.tar.gz (532.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

peerjs_py-0.1.0-py3-none-any.whl (37.2 kB view details)

Uploaded Python 3

File details

Details for the file peerjs_py-0.1.0.tar.gz.

File metadata

  • Download URL: peerjs_py-0.1.0.tar.gz
  • Upload date:
  • Size: 532.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for peerjs_py-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ea8f1e200ffe191fb864a07ca76ab146b0248b5de0c4de5e4c2831d1a31fc2ec
MD5 a70fd42200fa7e12f78a55f49fc187d7
BLAKE2b-256 cda2f9d0b0d6ed776054e1e0ab45b2e9a3be8b3086d3545d1da8504ecce20a46

See more details on using hashes here.

File details

Details for the file peerjs_py-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: peerjs_py-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 37.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for peerjs_py-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3ec400131efadbfe5268b09e55d704bf99ec398d44cbd973d4e2d52c46917e3f
MD5 84f090b96c7a7ad4aa736ef201c7eed2
BLAKE2b-256 ea73a06a44599c7e05ac954266a145b0a31610fde61cf54dacf6b6840e19730d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page