# 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.
Raw data
{
"_id": null,
"home_page": "https://github.com/sellpath/peerjs_py",
"name": "peerjs-py",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": null,
"keywords": "peerjs, webrtc, networking",
"author": "Mansu Kim",
"author_email": "Mansu Kim <mkim@sellpath.com>",
"download_url": "https://files.pythonhosted.org/packages/74/79/affc9dd24ee14aecebddc727bcd5147fac0c5c4aced006fcc690e989337d/peerjs_py-0.1.1.tar.gz",
"platform": null,
"description": "# peerjs-py\n\npeerjs-py is a Python implementation of the PeerJS library, allowing peer-to-peer connections in Python applications.\n\n## Installation\n\nTo install peerjs-py, use pip:\n\n```\npip install peerjs-py\n```\n\n## Usage\n\n### Connecting to a PeerJS Server\n\nFirst, import the necessary modules and create a Peer instance:\n\n```\npython\nfrom peerjs_python import Peer, PeerOptions\n```\n\n\n### Create a Peer instance\n```\npeer = Peer(PeerOptions(\nhost='your-peerjs-server.com',\nport=9000,\nsecure=True # Use this if your server uses HTTPS\n))\n```\n### Listen for the 'open' event to know when the connection to the server is established\n\n```\n@peer.on('open')\ndef on_open(id):\nprint(f\"My peer ID is: {id}\")\n```\n\n### Connecting to Another Peer\n\nTo connect to another peer and send text data:\n\n```\n#Establish a connection\nconn = peer.connect('remote-peer-id')\n\n@conn.on('open')\ndef on_open():\n # Connection is now open and ready for use\n conn.send('Hello, remote peer!')\n@conn.on('data')\ndef on_data(data):\n print(f\"Received: {data}\")\n\n```\n\n### Voice/Video Calls\n\nFor voice or video calls, you'll need to use additional libraries like PyAudio for audio processing. Here's a basic example:\n\n```\npython\nimport pyaudio\n# Initialize PyAudio\np = pyaudio.PyAudio()\n# Open a call connection\n\ncall = peer.call('remote-peer-id', {'audio': True, 'video': False})\n\n@call.on('stream')\ndef on_stream(stream):\n # Handle the incoming audio stream\n # This is a simplified example and would need more code to actually play the audio\n\n@peer.on('call')\ndef on_call(call):\n # Answer incoming calls automatically\n call.answer({'audio': True, 'video': False})\n\n@call.on('stream')\ndef on_stream(stream):\n # Handle the incoming audio stream\n```\n\n### File Transfer\n\nTo send files between peers:\n\n```\n#Sending a file\nwith open('file.txt', 'rb') as file:\n data = file.read()\n conn.send({'file': data, 'filename': 'file.txt'})\n\n# Receiving a file\n@conn.on('data')\ndef on_data(data):\n if isinstance(data, dict) and 'file' in data:\n with open(data['filename'], 'wb') as file:\n file.write(data['file'])\n print(f\"Received file: {data['filename']}\")\n\n```\n\n\n## Error Handling\n\nAlways implement error handling to manage potential connection issues:\n\n```\n\n@peer.on('error')\ndef on_error(error):\n print(f\"An error occurred: {error}\")\n```\n\n## Testing\n\nTo 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.\n\n### Running Tests\n\nWe use pytest as our testing framework. To run the tests, follow these steps:\n\n1. Ensure you have pytest installed:\n ```\n pip install pytest\n ```\n\n2. Navigate to the project root directory.\n\n3. Run the tests using pytest:\n ```\n pytest\n ```\n\n### End-to-End Tests\n\nWe have two types of end-to-end (e2e) tests to verify the functionality of peerjs-py in different scenarios:\n\n1. **Python Client to Python Client Test**\n This test checks the communication between two Python clients using peerjs-py.\n \n To run this test:\n ```\n ./e2e/run-e2e.sh\n ```\n\n2. **PeerJS Browser Client to Python Client Test**\n This test verifies the compatibility between a PeerJS browser client and a Python client using peerjs-py.\n \n To run this test:\n ```\n ./e2e/run-e2e-test-py.sh\n ```\n\n### Important Notes\n\n- Ensure your PeerJS signaling server is running and accessible before executing the tests.\n- The end-to-end tests may require additional setup or dependencies. Please refer to the respective script files for any specific instructions.\n- If you encounter any issues during testing, check your server configuration and network connectivity.\n\nBy 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.\n",
"bugtrack_url": null,
"license": null,
"summary": "A Python implementation of PeerJS",
"version": "0.1.1",
"project_urls": {
"Homepage": "https://github.com/sellpath/peerjs_py"
},
"split_keywords": [
"peerjs",
" webrtc",
" networking"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "f66d6e372ec9d991a5cd4ca520f2fa79c4cbc8aec638f42ad1ec4b29273f7b80",
"md5": "b24e6bac21c300159cc8ed55a295aed7",
"sha256": "d44de97da910163e6d7e316f9ab5caf4d6163206b06ad20a70abc2cd86d2c54c"
},
"downloads": -1,
"filename": "peerjs_py-0.1.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "b24e6bac21c300159cc8ed55a295aed7",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 37222,
"upload_time": "2024-09-24T05:13:40",
"upload_time_iso_8601": "2024-09-24T05:13:40.265055Z",
"url": "https://files.pythonhosted.org/packages/f6/6d/6e372ec9d991a5cd4ca520f2fa79c4cbc8aec638f42ad1ec4b29273f7b80/peerjs_py-0.1.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "7479affc9dd24ee14aecebddc727bcd5147fac0c5c4aced006fcc690e989337d",
"md5": "fff9600e8fedb31c065a4d6faffc1a78",
"sha256": "4e645872b4c0e96b68c7ee25f99ecbccc52854933b39fafb4d638516a27c752e"
},
"downloads": -1,
"filename": "peerjs_py-0.1.1.tar.gz",
"has_sig": false,
"md5_digest": "fff9600e8fedb31c065a4d6faffc1a78",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 532552,
"upload_time": "2024-09-24T05:13:42",
"upload_time_iso_8601": "2024-09-24T05:13:42.009890Z",
"url": "https://files.pythonhosted.org/packages/74/79/affc9dd24ee14aecebddc727bcd5147fac0c5c4aced006fcc690e989337d/peerjs_py-0.1.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-09-24 05:13:42",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "sellpath",
"github_project": "peerjs_py",
"github_not_found": true,
"lcname": "peerjs-py"
}