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 message broker (read more here). And Almanet uses NSQ to exchange messages between different sessions.

See more examples here.

Getting Started

Before install and run NSQD instance using this instruction.

pip install almanet --upgrade

Create a new session

session = almanet.new_session(<your nsqd tcp addresses>)

Join to your nsq network

await session.join()

Define your custom exception

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

Define your remote procedure to call

# First argument is a payload that was passed during invocation.
async def greeting(payload: str, **kwargs):
    """Procedure that returns greeting message"""
    if payload == 'guest':
        # you can raise custom exceptions and the caller will have an error
        raise Denied()
    return f'Hello, {payload}!'

Register your procedure in order to be called

await session.register('net.example.greeting', greeting)

Call the procedure net.examples.greeting with 'Aidar' argument. Raises TimeoutError if procedure not found or request timed out.

result = await session.call('net.example.greeting', 'Aidar')
print(result.payload)

Or catch remote procedure exceptions

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

Create net.example.notification consumer. almanet.consume returns tuple[iterable messages, function that can stop consuming]

messages_stream, stop_consumer = await session.consume(
    'net.example.notification', channel='test'
)

Start consuming messages and commit or rollback incoming message

async for message in messages_stream:
    try:
        print('new event', message.body)
        # at the end of iteration commit message
        await message.commit()
    except:
        # if something went wrong
        await message.rollback()

Publish message to net.example.notification topic with 'hello, world' argument

await session.produce('net.example.notification', 'hello, world')

Leave from network

await session.leave()

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.2.2.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

almanet-0.2.2-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for almanet-0.2.2.tar.gz
Algorithm Hash digest
SHA256 aa24f9ce831d517f8079fbcd70647a8df59d50e587f64427e4b7c31fbf751bb8
MD5 1adbb3cb4dd519076d0064758d9192af
BLAKE2b-256 36f614b450ddc7dec56aa801a04f869e879110be3b122f9c3445f9c2d7bc574a

See more details on using hashes here.

Provenance

File details

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

File metadata

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

File hashes

Hashes for almanet-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e83bca16f2c3d4713c1b101eeb0f9616bbf69aff901ed584de5879a0f4bb930b
MD5 0edd578e06bdd202081192efedf75ca7
BLAKE2b-256 005831ebbb5cc82c3abbd17b1d815fcc51b317c9bf51886700d35a23b47f2b1e

See more details on using hashes here.

Provenance

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