#接口 #wasmCloud #能力 #消息代理 #API绑定 #能力提供者

wasmcloud-interface-messaging

wasmCloud消息能力接口库,wasmcloud:messaging

18个版本 (10个重大变更)

0.11.0 2023年9月19日
0.10.0 2023年7月20日
0.9.0 2023年4月12日
0.8.1 2022年11月23日
0.2.0 2021年10月24日

WebAssembly中排名第120


用于dtbh_interface

Apache-2.0 和可能 LGPL-3.0-or-later

31KB
583

crates.io  TinyGo版本

wasmCloud消息接口

这是针对 wasmcloud:messaging 合同的接口。该合同是对消息代理概念的简单抽象。该合同不包含对交付保证(例如,“最多一次”与“至少一次”)或消息持久性等内容的控制、旋钮或设置。此类详细信息由个别提供者负责。

能力提供者实现

以下是对 wasmcloud:messaging 合同的实现的列表。如果您有社区/开源版本,请随时提交PR添加您的实现。

名称 供应商 描述
NATS wasmCloud NATS代理提供wasmCloud消息提供者

示例用法

🦀 Rust

实现 Messaging.HandleMessage 操作

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(())
    }
}

通过 wasmcloud:messaging 提供者发送消息,不期望回复

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(())
    }
}

通过 wasmcloud:messaging 提供者发送消息,等待一秒回复

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

实现 Messaging.HandleMessage 操作

 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)})
}

通过 wasmcloud:messaging 提供者发送消息,不期望回复

 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,
   })
}

通过 wasmcloud:messaging 提供者发送消息,等待一秒回复

 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)})
}

依赖关系

~12–30MB
~494K SLoC