Publishing to a topic
pmesh by default uses the ev
stream for events to ensure durability by default, meaning any subject name provided will be prefixed with ev.
before being emitted to the NATS server.
For more advanced use cases, you can use the raw.
prefix to disable this behaviour and publish messages to any NATS subject name as is.
Publishing a message
For convenience, pmesh provides an API to publish messages to the NATS server, available at the /publish/:topic
endpoint.
The request body and headers are directly forwarded to the NATS server as a message.
Although it will be explored in more detail later, the most simple way of making internal requests to pmesh is via HTTP using the hostname pm3
.
This hostname is a special alias that resolves to the pmesh instance’s internal IP address, but only authenticates requests from the local network.
For this endpoint, the full URL would be http://pm3/publish/topic
.
Since we have a project with a my-api
service, we can actually start publishing messages and receiving responses right away without defining any runners.
First let’s define a POST route in our project.
Now we can simply use curl
to send a request to the pmesh instance.
The logs will show the original request headers, as well as the message queue state.
For each service in the manifest, there is also a corresponding implicit runner that subscribes to the svc.<name>.>
and raw.svc.<name>.>
subjects and forwards the messages to the service’s HTTP implementation, replacing the .
delimiter with /
to form a path.
Receiving a response
Cool, but where’s our response?
Once the request is finished, if the message originates from a stream, pmesh will store the response in a special key-value store, which is also conveniently exposed as an endpoint at /result/:stream/:seq
.
You can think of what we received earlier as a promise.
We could also use the raw.
prefix to publish a regular NATS message, where the response will be directly sent back to us as a reply.
Similarly, we can use the nats
CLI to send these requests.