Skip to main content

Web Messaging Protocol

Project description

almanet

Web Messaging Protocol is an open application level protocol that provides two messaging patterns:

  • Routed Remote Procedure Calls (RPC)
  • Produce & Consume

NSQ is a realtime distributed queue like message broker.

Almanet uses NSQ to exchange messages between different sessions.

Quick Start

Before install and run NSQD instance using this instruction.

Then install almanet PyPI package

pip install almanet

or

poetry add almanet

Create a new file and

import almanet

Create your own Microservice

Explanation of the code that defines and runs a simple microservice

Define your instance of microservice

example_service = almanet.new_service(
    'localhost:4150',
    prefix="net.example"
)

Arguments:

  • the TCP addresses of the NSQ instances
  • prefix for the service's procedures, helping in identifying and organizing them

Define your custom exception

class denied(almanet.rpc_error):
    """Custom RPC exception"""

This custom exception can be raised within procedures to signal specific error conditions to the caller.

Define your remote procedure to call

@example_service.procedure
async def greeting(
    session: almanet.Almanet,
    payload: str,
) -> str:
    """Procedure that returns greeting message"""
    if payload == "guest":
        raise denied()
    return f"Hello, {payload}!"

Decorator @example_service.procedure registers the greeting function as a remote procedure for the example_service.

Arguments:

  • payload is a data that was passed during invocation.
  • session is a joined service, instance of almanet.Almanet

It raises the custom denied exception, indicating that this payload is not allowed if payload is "guest". Otherwise, it returns a greeting message.

At the end of the file

if __name__ == "__main__":
    example_service.serve()

Starts the service, making it ready to handle incoming RPC requests.

Finally

Run your module using the python command

Call your Microservice

Explanation of the code for creating a new session, calling a remote procedure, and handling potential exceptions during the invocation.

Create a new session

session = almanet.new_session('localhost:4150')

Arguments:

  • the TCP addresses of the NSQ instances

Calling the Remote Procedure

async with session:
    result = await session.call("net.example.greeting", "Aidar")
    print(result.payload)

async with session ensures that the session is properly managed and closed after use. Calls the remote procedure net.example.greeting with the payload "Aidar". Raises TimeoutError if procedure not found or request timed out. result.payload contains the result of the procedure execution.

Catching remote procedure exceptions

async with session:
    try:
        await session.call("net.example.greeting", "guest")
    except almanet.rpc_error as e:
        print("during call net.example.greeting('guest'):", e)

The try block attempts to call the net.example.greeting procedure with the payload "guest". If an exception occurs during the call, specifically an almanet.rpc_error, it is caught by the except block.

Finally

Run your module using the python command


See the full examples in ./examples directory.

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

almanet-0.3.0.tar.gz (13.7 kB view details)

Uploaded Source

Built Distribution

almanet-0.3.0-py3-none-any.whl (16.3 kB view details)

Uploaded Python 3

File details

Details for the file almanet-0.3.0.tar.gz.

File metadata

  • Download URL: almanet-0.3.0.tar.gz
  • Upload date:
  • Size: 13.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.3 Darwin/22.6.0

File hashes

Hashes for almanet-0.3.0.tar.gz
Algorithm Hash digest
SHA256 0c9045fa239e3047cf8e033d1b08ebc9c819fa7c8b7fdc5a77b38b0dbca9c34e
MD5 a271a8044daba58917afa94848239ce0
BLAKE2b-256 da578d593c47e9c58253da8882bc40c201715a666129ecb79bb501bde242453b

See more details on using hashes here.

File details

Details for the file almanet-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: almanet-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 16.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.3 Darwin/22.6.0

File hashes

Hashes for almanet-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fd1876a426ef82687560905afc41ac9567c273a03d17be45b9276f38ecae6d8a
MD5 291cb44cf9c3275319bb306e83b85acc
BLAKE2b-256 ed5a6e8176da2d30f33a6b80d8280aa60b9526ca1213ff16e55e92fc867a6bfe

See more details on using hashes here.

Supported by

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