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.

Create a new session

session = almanet.new_session()

Join to your nsq network

await session.join(<your nsqd tcp addresses>)

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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: almanet-0.2.1.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.1.tar.gz
Algorithm Hash digest
SHA256 e32f6862c5e6acdd11a2fbc8df7c9881dd444ef4fb79ccce7486457d8818a03b
MD5 85f3f95632f2919b0cdaba9c64cfb3d0
BLAKE2b-256 0083a583dfbbe3691e0aa880a5e26a58830f8532137bf62ee496665d370e5b96

See more details on using hashes here.

File details

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

File metadata

  • Download URL: almanet-0.2.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e5ae56bbc7930bfa67397437522c5cd8652e18ae18de1a10e894f7911a349e77
MD5 29a69bb2d057a57b8e28a839c6d2474c
BLAKE2b-256 f2fb8bc958f22d5baaa11717f33eb63219c320084a80c1492cde721311f0bc6a

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