A message subject is a string that identifies the type of a message in a messaging system. In NATS, publishers choose the naming scheme, and subscribers listen for subjects they care about. section 21.2
Naming a to-do event
The to-do application publishes a new-item event with the subject events.todo.newitem. A subscriber listening to that exact subject receives the event, while publishing still succeeds when no subscriber is connected. section 21.2
Use message subjects to support asynchronous features:
- Subscribe multiple handlers to the same subject so each can react independently, such as saving data and writing an audit trail. section 21.4
- Keep subject names meaningful and consistent; events.todo.newitem communicates both the application area and event type. section 21.2
# run a subscriber listening for "events.todo.newitem" messages docker run -d --name todo-sub ` --network todo-list_app-net ` diamol/nats-sub:2e events.todo.newitem # check the subscriber logs: docker logs todo-sub # browse to http:/./localhost:8080 and add some new items # check that the new item events are published: docker logs todo-sub
Run a subscriber for events.todo.newitem, add items through the application, and inspect the logs to verify that matching messages arrive. section 21.2
