1 PubSub
Ralph Schaer edited this page 2026-10-30 16:08:52 +01:00

Publish/Subscribe

The broker routes publications to subscribed WAMP clients and annotated Spring bean listeners. Enable Feature.BROKER (the default).

Java listeners

import org.springframework.stereotype.Service;
import ch.rasc.wamp2spring.annotation.WampListener;
import ch.rasc.wamp2spring.message.EventMessage;
import ch.rasc.wamp2spring.pubsub.MatchPolicy;

@Service
public class NewsListener {
  @WampListener({"news.local", "news.global"})
  public void onNews(String headline) {
    System.out.println(headline);
  }

  @WampListener(topic = "sensor..temperature", match = MatchPolicy.WILDCARD)
  public void onTemperature(double temperature, EventMessage event) {
    System.out.println(event.getTopic() + ": " + temperature);
  }
}

value and topic are aliases. An annotation with no topics uses beanName.methodName. Matching defaults to EXACT; PREFIX uses a string prefix and WILDCARD uses empty URI components. A pattern event includes the actual topic in EventMessage.getTopic().

Payload arguments and metadata resolve as described in RPC, using EventMessage instead of CallMessage. Listener return values are ignored; listener exceptions are logged. Use WampPublisher for a response event.

Publish from Java

Inject ch.rasc.wamp2spring.WampPublisher into a Spring bean:

Method Recipients
publishToAll(topic, arguments) All eligible subscribers of the topic
publishTo(sessionId, topic, arguments) A single subscribed WAMP session
publishTo(sessionIds, topic, arguments) A collection of subscribed WAMP sessions
publishToAllExcept(sessionIdOrIds, topic, arguments) Subscribers except the selected WAMP sessions
publish(PublishMessage) Explicit publication options

The convenience methods accept varargs, a collection of positional arguments, or a map of keyword arguments. Recipients must already subscribe to the topic.

publisher.publishToAll("news.local", "New service online");
publisher.publishTo(sessionId, "account.changed", Map.of("status", "ready"));
publisher.publish(publisher.publishMessageBuilder("sensor.office.temperature")
    .arguments(List.of(21.5))
    .retain()
    .notExcludeMe()
    .build());

Use java.util.List and java.util.Map for this example. Publications exclude their publisher by default. For publications originating in WampPublisher, that default also skips all annotated Java listeners. Set .notExcludeMe() to deliver server-originated publications to local listeners. The default broker channel is synchronous, so local listeners then run on the publishing thread.

Client publication options

Option Effect
acknowledge: true Request a PUBLISHED response with a publication ID
exclude_me: false Include the publishing client if it subscribes
disclose_me: true Include publisher identity details
eligible, exclude Include/exclude WAMP session IDs
eligible_authid, exclude_authid Include/exclude authenticated IDs
eligible_authrole, exclude_authrole Include/exclude authentication roles
retain: true Ask the event store to retain this publication; see the default-store limitation below
rkey Select a subscription shard

All supplied eligibility filters must pass; exclusion filters then remove matching recipients. These filters operate on remote subscribers, not Java listener methods. Client publications to the reserved wamp. namespace are rejected; clients can subscribe to the router's meta topics.

For sharded subscriptions, subscribers negotiate sharded_subscription and set SUBSCRIBE.Options.nkey. A supporting publisher supplies a string rkey. The registry hashes it across the sorted distinct node keys of matching subscriptions. Non-sharded subscriptions still receive matching traffic; without an rkey, sharded subscriptions are omitted.

Retention

Publish with retain: true or the builder's .retain(). Clients request retained events with SUBSCRIBE.Options.get_retained: true; matching retained publications are delivered with the retained marker.

The default MemoryEventStore keeps the latest retained publication per topic only when it originates on the server (no transport session ID). A client's retain request is passed to the store but is not retained by this default implementation. Retained state is held in memory, without expiry or persistence.

Supply an EventStore Spring bean to change that policy. Implement retain(PublishMessage) and getRetained(DestinationMatch), including the matching behavior your application needs.

History

With BROKER_EVENT_HISTORY enabled, the broker stores publications for matching subscriptions, independently of retain. The default store keeps a list per subscription ID, with no size or age limit, and deletes it when that subscription is deleted. It does not maintain history for topics with no matching subscription, and restart loses the data.

Read history through wamp.subscription.get_events; see MetaAPIs. A custom store should also implement storeHistoryEvent, getHistory, and deleteHistory. Their interface defaults do nothing or return an empty list, so a retention-only store does not provide history automatically.

Administrative Java code can call PubSubMessageHandler.revokeSubscription(long subscriptionId, String reason). It removes subscribers and sends extended UNSUBSCRIBED messages to supporting peers. It is not exposed as a wamp.subscription.revoke RPC.

Source: PubSubMessageHandler, MemoryEventStore.