Skip to main content

Python SDK for EnSync Engine

Project description

EnSync Python SDK

A Python SDK for interacting with the EnSync messaging service. This SDK provides functionality for connecting to EnSync, publishing and subscribing to events, and handling encrypted messages.

Installation

pip install -e .

Features

  • WebSocket connection management with automatic reconnection
  • Event publishing with traditional and hybrid encryption
  • Event subscription with customizable handlers
  • Support for acknowledging, rejecting, and deferring events
  • Hybrid encryption for improved performance with multiple recipients

Usage

Creating a Client

import asyncio
from ensync import EnSyncEngine

async def main():
    # Initialize the client
    client = EnSyncEngine("ws://localhost:8082")
    
    # Connect and authenticate
    await client.create_client("your_access_key")
    
    # Close the connection when done
    await client.close()

asyncio.run(main())

Publishing Events

# Publish an event
response = await client.publish(
    "your/event/name",
    ["recipient_public_key"],
    {"key": "value"},  # Payload
    {"persist": True, "headers": {}}  # Metadata
)

Subscribing to Events

# Subscribe to an event
subscription = await client.subscribe("your/event/name", {"autoAck": False})

# Add an event handler
async def handle_event(event):
    print(f"Received event: {event}")
    
    # Acknowledge the event
    if "idem" in event:
        await subscription.acknowledge(event["idem"])

# Register the handler
remove_handler = subscription.on(handle_event)

# When done, remove the handler
remove_handler()

Event Management

# Acknowledge an event
await subscription.acknowledge(event_id)

# Reject an event
await subscription.reject(event_id, "Rejection reason")

# Defer an event
await subscription.defer(event_id, 5000, "Deferred for 5 seconds")

# Pause a subscription
await subscription.pause("Paused for maintenance")

# Resume a subscription
await subscription.resume()

# Replay a specific event
await subscription.replay(event_id)

Environment Variables

The test scripts use the following environment variables:

  • ENSYNC_ACCESS_KEY - Access key for the publisher
  • CLIENT_ACCESS_KEY - Access key for the subscriber
  • APP_SECRET_KEY - Optional secret key for decryption
  • EVENT_TO_PUBLISH - Event name to publish
  • EVENT_TO_SUBSCRIBE - Event name to subscribe to
  • RECEIVER_IDENTIFICATION_NUMBER - Recipient's public key
  • REPLAY_EVENT_ID - Optional ID of an event to replay

Examples

See the ensync/tests directory for example publisher and subscriber scripts.

License

Copyright © EnSync Team

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

ensync_sdk-0.1.0-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ensync_sdk-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for ensync_sdk-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 56209d68a6d05b5859331aa9fda7a8d14d8ed7905948a08527c0f0320d143dcb
MD5 ca7c5603244d79ff22fa6a35d14b43ee
BLAKE2b-256 01d3fdc2d8a97cb93f86f52b0075842b98331fb021702ee4f3937fdef85ebd56

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