JVM - topic.publish()

Publish an event (push based message) to a topic.

import io.nitric.Nitric;
import io.nitric.api.events.v0.NitricEvent;
import io.nitric.resources.TopicPermission;
public class Application {
public static void main(String[] args) {
var topic = Nitric.INSTANCE.topic("new-user").with(TopicPermission.Publishing);
topic.publish(new NitricEvent(Map.of("message", "new user created"), "1234", "none"));
Nitric.INSTANCE.run();
}
}

Parameters


  • Name
    event
    Required
    Required
    Type
    Event or Map<String, Any>
    Description

    The event to publish to the topic.

    • Name
      payload
      Required
      Required
      Type
      Map<String, Any>
      Description

      Payload to send with the event.

    • Name
      id
      Optional
      Optional
      Type
      String
      Description

      The unique ID to apply to the event. If an ID is not supplied a UUIDv4 is generated.

    • Name
      payloadType
      Optional
      Optional
      Type
      String
      Description

      A hint to the type of payload supplied.

Examples

Publish a topic

import io.nitric.Nitric;
import io.nitric.api.events.v0.NitricEvent;
import io.nitric.resources.TopicPermission;
public class Application {
public static void main(String[] args) {
var topic = Nitric.INSTANCE.topic("topic").with(TopicPermission.Publishing);
topic.publish(new NitricEvent(Map.of("message", "New user created"), "1234", "none"));
Nitric.INSTANCE.run();
}
}

Send a message with a delay

import io.nitric.Nitric;
import io.nitric.api.events.v0.NitricEvent;
import io.nitric.api.events.v0.PublishOptions;
import io.nitric.resources.TopicPermission;
public class Application {
public static void main(String[] args) {
var topic = Nitric.INSTANCE.topic("topic").with(TopicPermission.Publishing);
topic.publish(
new NitricEvent(Map.of("message", "New user created"), "1234", "none"),
PublishOptions.WithDelay(600) // Send with a delay of 600 seconds (10 minutes)
);
Nitric.INSTANCE.run();
}
}

Notes

  • If an id is not supplied with an event a UUID(v4) will be generated for you.
  • A function may subscribe to OR publish to a topic but not both.
Last updated on Dec 19, 2024