Skip to main content

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_ID or PHYGRID_DEVICE_ID: Device identifier
  • ACCESS_KEY or PHYGRID_DEVICE_KEY: Access key
  • PHYHUB_URL or PHYHUB_REGION: Server URL or region (eu, us, etc.)
  • TWIN_ID or INSTANCE_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 client
  • aiortc - WebRTC implementation
  • opencv-python - Webcam capture
  • aiohttp - 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

phystack_hub_client-0.2.6.tar.gz (40.5 kB view details)

Uploaded Source

Built Distribution

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

phystack_hub_client-0.2.6-py3-none-any.whl (41.1 kB view details)

Uploaded Python 3

File details

Details for the file phystack_hub_client-0.2.6.tar.gz.

File metadata

  • Download URL: phystack_hub_client-0.2.6.tar.gz
  • Upload date:
  • Size: 40.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for phystack_hub_client-0.2.6.tar.gz
Algorithm Hash digest
SHA256 062f2f0e86b103d264d5883bd9aada0fc06bd87d35dd367b123c5a1de86fea1e
MD5 9480ebf8ca9ff6e8d8b560dbff4847b0
BLAKE2b-256 65ce29e44b48c19c40953e7405bf3d3a569e6d407e64489e9a568be99dfa29ae

See more details on using hashes here.

Provenance

The following attestation bundles were made for phystack_hub_client-0.2.6.tar.gz:

Publisher: build-and-publish.yml on phystack/phystack-pypi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file phystack_hub_client-0.2.6-py3-none-any.whl.

File metadata

File hashes

Hashes for phystack_hub_client-0.2.6-py3-none-any.whl
Algorithm Hash digest
SHA256 a6cf174ac68318c0a20d72ee8f23d57ed00f303d5be1e4ea2905a812792da2aa
MD5 49956416aa328d5d6dd7fc05c14b0097
BLAKE2b-256 bc1924107db83f6f1c8f78e0a3b98e800ba68ba0ff2a98bc1228da371f3d4968

See more details on using hashes here.

Provenance

The following attestation bundles were made for phystack_hub_client-0.2.6-py3-none-any.whl:

Publisher: build-and-publish.yml on phystack/phystack-pypi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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