#wasmcloud #capability #interface

wasmcloud-interface-messaging

Interface library for the wasmCloud messaging capability, wasmcloud:messaging

18 releases (10 breaking)

0.11.0 Sep 19, 2023
0.10.0 Jul 20, 2023
0.9.0 Apr 12, 2023
0.8.1 Nov 23, 2022
0.2.0 Oct 24, 2021

#72 in WebAssembly

Download history 38/week @ 2023-10-18 31/week @ 2023-10-25 12/week @ 2023-11-01 28/week @ 2023-11-08 12/week @ 2023-11-15 37/week @ 2023-11-22 62/week @ 2023-11-29 44/week @ 2023-12-06 30/week @ 2023-12-13 23/week @ 2023-12-20 101/week @ 2023-12-27 147/week @ 2024-01-03 121/week @ 2024-01-10 34/week @ 2024-01-17 20/week @ 2024-01-24 373/week @ 2024-01-31

610 downloads per month
Used in dtbh_interface

Apache-2.0 and maybe LGPL-3.0-or-later

31KB
583 lines

crates.io  TinyGo Version

wasmCloud Messaging Interface

This is the interface for the wasmcloud:messaging contract. This contract is a very simple abstraction over the concept of a message broker. This contract does not have controls or knobs or settings for things like delivery guarantees (e.g. "at most once" vs "at least once"), persistence of messages, etc. Such details are the responsibility of individual providers.

Capability Provider Implementations

The following is a list of implementations of the wasmcloud:messaging contract. Feel free to submit a PR adding your implementation if you have a community/open source version.

Name Vendor Description
NATS wasmCloud wasmCloud Messaging Provider for the NATS broker

Example Usage

🦀 Rust

Implementing the Messaging.HandleMessage operation

use wasmbus_rpc::actor::prelude::*;
use wasmcloud_interface_logging::info;
use wasmcloud_interface_messaging::{MessageSubscriber, MessageSubscriberReceiver, SubMessage};

#[derive(Debug, Default, Actor, HealthResponder)]
#[services(Actor, MessageSubscriber)]
struct LogMessagingActor {}

#[async_trait]
impl MessageSubscriber for LogMessagingActor {
    /// Handle a message received on a subscription
    async fn handle_message(&self, _ctx: &Context, msg: &SubMessage) -> RpcResult<()> {
        info!("Received message: {:?}", msg);
        Ok(())
    }
}

Sending a message via a wasmcloud:messaging provider without expecting a reply

use wasmbus_rpc::actor::prelude::*;
use wasmcloud_interface_messaging::{Messaging, MessagingSender, PubMessage};
async fn publish_message(ctx: &Context, subject: &str, body: &[u8]) -> RpcResult<()> {
    let provider = MessagingSender::new();
    if let Err(e) = provider
        .publish(
            ctx,
            &PubMessage {
                body: body.to_vec(),
                reply_to: None,
                subject: subject.to_owned(),
            },
        )
        .await
    {
        Err(format!("Could not publish message {}", e.to_string()).into())
    } else {
        Ok(())
    }
}

Sending a message via a wasmcloud:messaging provider, waiting one second for a reply

use wasmbus_rpc::actor::prelude::*;
use wasmcloud_interface_messaging::{Messaging, MessagingSender, RequestMessage};
async fn message_request(ctx: &Context, subject: &str, body: &[u8]) -> RpcResult<()> {
    let provider = MessagingSender::new();
    if let Err(e) = provider
        .request(
            ctx,
            &RequestMessage {
                body: body.to_vec(),
                subject: subject.to_owned(),
                timeout_ms: 1_000,
            },
        )
        .await
    {
        Err(format!("Could not request message {}", e.to_string()).into())
    } else {
        Ok(())
    }
}

🐭 Golang

Implementing the Messaging.HandleMessage operation

 import (
    "github.com/wasmcloud/actor-tinygo"
    logging "github.com/wasmcloud/interfaces/logging/tinygo"
    messaging "github.com/wasmcloud/interfaces/messaging/tinygo"
 )
 
type Actor struct {
   logger *logging.LoggingSender
}

func main() {
   me := Actor{
      logger: logging.NewProviderLogging(),
   }

   actor.RegisterHandlers(messaging.MessageSubscriberHandler(&me))
}

func (e *Actor) HandleMessage(ctx *actor.Context, msg messaging.SubMessage) error {
   return e.logger.WriteLog(ctx, logging.LogEntry{Level: "info", Text: string(msg.Body)})
}

Sending a message via a wasmcloud:messaging provider without expecting a reply

 import (
    "github.com/wasmcloud/actor-tinygo"
    messaging "github.com/wasmcloud/interfaces/messaging/tinygo"
)

func PublishMessage(ctx *actor.Context, subj string, body []byte) error {
   client := messaging.NewProviderMessaging()
   return client.Publish(ctx, messaging.PubMessage{
      Subject: subj,
      ReplyTo: "",
      Body:    body,
   })
}

Sending a message via a wasmcloud:messaging provider, waiting one second for a reply

 import (
    "github.com/wasmcloud/actor-tinygo"
    logging "github.com/wasmcloud/interfaces/logging/tinygo"
    messaging "github.com/wasmcloud/interfaces/messaging/tinygo"
)

var logger *logging.NewProviderLogging()

func MessageRequest(ctx *actor.Context, subj string, body []byte) error {
    client := messaging.NewProviderMessaging()

    reply, _ := client.Request(ctx, messaging.RequestMessage{
       Subject:   subj,
       Body:      body,
       TimeoutMs: 1000,
    })

    return logger.WriteLog(ctx, logging.LogEntry{Level: "info", Text: "Response: " + string(reply.Body)})
}

Dependencies

~13–31MB
~504K SLoC