Python client for PhyHub - events, actions, DataChannel, and MediaStream
Project description
Phystack Hub Client (Python)
Python client for PhyHub that provides:
- Events and Actions via twin messaging
- WebRTC DataChannel for P2P data
- WebRTC MediaStream for P2P video/audio (with webcam support)
Installation
pip install phystack-hub-client
Or install from source:
pip install -e .
Usage
Basic Connection
import asyncio
from phystack.hub_client import PhyHubClient
async def main():
# Create client from environment variables
async with PhyHubClient.from_env() as client:
instance = await client.get_instance()
print(f"Connected as twin: {instance.id}")
asyncio.run(main())
Environment Variables
DEVICE_IDorPHYGRID_DEVICE_ID: Device identifierACCESS_KEYorPHYGRID_DEVICE_KEY: Access keyPHYHUB_URLorPHYHUB_REGION: Server URL or region (eu, us, etc.)TWIN_IDorINSTANCE_ID: Optional specific twin ID
Events
# Listen for events
instance.on("my-event", lambda data, respond: print(f"Received: {data}"))
# Send events to a peer
instance.to(peer_twin_id).emit("my-event", {"message": "hello"})
Actions (Request-Response)
def on_result(result):
print(f"Response: {result.status} - {result.message}")
# Send action with callback
instance.to(peer_twin_id).emit("my-action", {"command": "process"}, callback=on_result)
# Handle actions
def handle_action(data, respond):
if respond:
respond(TwinMessageResult(
status=TwinMessageResultStatus.SUCCESS,
message="Processed",
data={"result": "done"}
))
instance.on("my-action", handle_action)
WebRTC DataChannel
# Create DataChannel connection
dc = await client.create_data_channel(peer_twin_id, is_initiator=True)
# Send data
dc.send({"message": "hello via WebRTC"})
# Receive data
dc.on_message(lambda data: print(f"Received: {data}"))
# Close when done
dc.close()
WebRTC MediaStream (with Webcam)
# Create MediaStream connection (streams from webcam)
stream = await client.create_media_stream(peer_twin_id, is_initiator=True)
# Check if streaming
print(f"Streaming: {stream.is_open()}")
print(f"Has video: {stream.video_track is not None}")
# Close when done
stream.close()
Testing
Python-Node Interoperability Test
Test communication between Python and Node.js hub-client implementations:
# Setup credentials
./scripts/python-node-test.sh setup
# Run test (Python initiator, Node.js responder)
./scripts/python-node-test.sh run
# Run reversed (Node.js initiator, Python responder)
./scripts/python-node-test.sh run-rev
Browser Test Backend
Use the Python client as initiator for the browser-based responder:
export DEVICE_ID=your-device-id
export ACCESS_KEY=your-access-key
export PEER_TWIN_ID=browser-responder-twin-id
export PHYHUB_REGION=eu
python examples/browser_test_backend.py
Dependencies
python-socketio[asyncio]- Socket.IO clientaiortc- WebRTC implementationopencv-python- Webcam captureaiohttp- HTTP client
License
MIT
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file phystack_hub_client-0.2.3.tar.gz.
File metadata
- Download URL: phystack_hub_client-0.2.3.tar.gz
- Upload date:
- Size: 39.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f69a21fc88a136226757e8e10576ff9556397f4fc23322d3bcbd3244b2a8371b
|
|
| MD5 |
919fbde0987db606c79c578655fbbf2e
|
|
| BLAKE2b-256 |
4eb46c99f2966ea56e14ed1084e0d2b2073768a8468f0f9f55015a80cc790ef5
|
Provenance
The following attestation bundles were made for phystack_hub_client-0.2.3.tar.gz:
Publisher:
build-and-publish.yml on phystack/phystack-pypi
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
phystack_hub_client-0.2.3.tar.gz -
Subject digest:
f69a21fc88a136226757e8e10576ff9556397f4fc23322d3bcbd3244b2a8371b - Sigstore transparency entry: 1254663850
- Sigstore integration time:
-
Permalink:
phystack/phystack-pypi@9a630844dd02848ffe0fa4f004fd73f581f78022 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/phystack
-
Access:
internal
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build-and-publish.yml@9a630844dd02848ffe0fa4f004fd73f581f78022 -
Trigger Event:
push
-
Statement type:
File details
Details for the file phystack_hub_client-0.2.3-py3-none-any.whl.
File metadata
- Download URL: phystack_hub_client-0.2.3-py3-none-any.whl
- Upload date:
- Size: 40.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4d2c200084fa8098c6d42990d1fe660a8d9f988e115df7e26b4ff9292aa3875b
|
|
| MD5 |
287b26798ae3487ce7b37428ac19346c
|
|
| BLAKE2b-256 |
3c58182b2d3e77af72518a452125911f922eee891b9dbfdfcac4e43d4b46042f
|
Provenance
The following attestation bundles were made for phystack_hub_client-0.2.3-py3-none-any.whl:
Publisher:
build-and-publish.yml on phystack/phystack-pypi
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
phystack_hub_client-0.2.3-py3-none-any.whl -
Subject digest:
4d2c200084fa8098c6d42990d1fe660a8d9f988e115df7e26b4ff9292aa3875b - Sigstore transparency entry: 1254663923
- Sigstore integration time:
-
Permalink:
phystack/phystack-pypi@9a630844dd02848ffe0fa4f004fd73f581f78022 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/phystack
-
Access:
internal
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build-and-publish.yml@9a630844dd02848ffe0fa4f004fd73f581f78022 -
Trigger Event:
push
-
Statement type: