makinacorpus/apubsub

提供异步的类似PubSub API,具有Drupal实现

4.0.2 2018-10-26 09:47 UTC

This package is auto-updated.

Last update: 2024-08-29 15:11:04 UTC


README

提供异步类似PubSub的通用API。允许将类型化的消息发送到通信渠道,这些渠道会将消息投递到订阅的收件箱。每个订阅都可以标记或取消标记其消息为已读或未读,通过基本属性进行过滤以检索它们。

它的目标是快速并且能够处理非常高的消息量。

警告:此文档已过时,尽管解释的概念没有改变,但实现的一些细节已经改变,将在稳定版本发布之前尽快更新

入门

为了遵循此教程,您需要一个完整设置和工作的后端。设置和对象实例化将取决于所选择的后端,因此我们将假设已创建 $pubsub 对象。

// This is our object:
$pubsub = new SomePubSubBackend();

创建频道

// Simply create the new channel
// The channel instance is returned by the createChannel() method
$channel_foo = $pubsub->createChannel("foo");
$channel_bar = $pubsub->createChannel("bar");
$channel_baz = $pubsub->createChannel("baz");

创建消息

创建消息可能是所有操作中最简单的。

// Retrieve our channel
$channel = $pubsub->getChannel("foo");

// And yet it is simple as that
$channel->send("Hello, World!");

使用SubscriberInterface

订阅者是一个助手,它会为您保留业务标识符和您为其创建的订阅之间的业务映射。这是使用此API的最简单方法,并且适用于大多数目的。

订阅者是一个瞬态对象,它不会在数据库中实现。一旦您让它订阅频道,映射就会被保留,但一旦您取消订阅,映射就会被删除。

// Create a new subscriber
$subscriber = $pubsub->getSubscriber("my_business_id");

// Subscribe to a channel, unlike the subscription, subscribing via the
// subscriber object will directly activate the new subscription
$subscriber->subscribe("foo");
$subscriber->subscribe("bar");
$subscriber->subscribe("baz");

// Fetching subscriber messages
$messages = $subscriber->fetch();

请注意,消息将从一个活动的所有订阅中检索,并按创建日期排序。

如果您需要对订阅者的订阅执行更复杂的操作,您可以检索订阅实例并直接与它们一起工作。

$subscription = $subscriber->getSubscription("bar");

// For example, deactivate a subscription without deleting it
$subscription->deactivate();

// Or delete another one
$subscriber
    ->getSubscription("baz")
    ->delete();

您需要使用订阅者而不是直接处理订阅的真实用例是,当您需要同时从多个频道检索消息时(例如,社交网络中的用户通知)。

直接与订阅一起工作

此外,您可以绕过订阅者实例,只与订阅一起工作。如果您可以在业务层中存储订阅标识符,则建议使用此方法:它将提供最佳性能,因为它不需要保留自己的映射。

订阅频道基本上是两件事:首先创建一个与该频道相关联的订阅 - 订阅不能没有频道 - 然后激活它。

订阅实例将始终携带一个标识符,其类型取决于后端(但始终将是PHP原始标量类型)。此标识符是您需要在业务层中存储的链接,以便能够稍后检索消息或取消其激活:您需要自己在上层存储它。

// Retrieve our channel
$channel = $pubsub->getChannel("foo");

// Create the new subscription
$subscription = $channel->subscribe();

// Per default, the subscription is inactive in order to avoid data bloat
// when a subscription is accidentally created
$subscription->activate();

// At this point, you must keep the subscription identifier, else it will
// be lost forever
$subscriptionId = $subscription->getId();

检索新消息

可以在每个订阅或订阅者基础上检索新消息。

// Retrieve the subscription: the identifier here is the one you kept
// and stored when you created the subscription
$subscription = $pubsub->getSubscription($subscriptionId);

// Fetch operation will only get new messages. The backend may or may not
// let you keep the messages stored depending on both the backend capability
// and configuration
$messages = $subscription->fetch();

如果您正在处理用户通知等,并且希望将它们持久化一段时间,您需要将消息存储到自己的业务API中。

历史记录

  • master分支是新功能的主要开发分支

  • 0.3版本删除了所有特定的对象实现,为所有对象添加了通用游标,再次减少了代码。代价是在更新和删除操作中性能下降,但在批量操作中性能提升。

  • 0.2分支几乎与0.1分支兼容,并包含许多内部改进,以及代码数量的显著减少。同时,它也清理了Drupal模块

  • 0.1分支是遗留的初始版本,保留它是为了与现在已投入生产的某些原型和测试项目保持兼容性