Simple example
A common interaction between services is a request and a response. We’ll test that here by using a Remit request
and endpoint
.
Here, we create two files: an endpoint called 'hello'
which, when hit, returns 'Hello, NAME!'
and a requester which will hit the endpoint with some data and log the result.
Boom. Done.
If you’re happy, take a look at the Concepts page to get affiliated with the four base types that Remit provides you with. If you’d like a more thorough explanation of the above, read on.
Create a project
We’ll assume you have a local RabbitMQ running (following the installation instructions).
First, let’s create a new project and install Remit. In the terminal:
Create an endpoint
Sorted. Now let’s create a new file called endpoint.js
:
Super simple! We’ve got a file ready that boots an endpoint!
When the endpoint boots, Remit will create a RabbitMQ “queue” for incoming messages. Requests will place a message in the endpoint’s queue and provide an address of sorts to receive the reply on.
Create a request
Let’s now create another file called request.js
which we’ll use to send a request to our 'hello'
endpoint:
When that file is run, it’ll send a request to our 'hello'
endpoint.
When the requester boots, Remit creates a temporary “reply” queue for incoming replies. Because of this small overhead, it’s best to create a single request and re-use it multiple times.
Awesome. Let’s give it a try.
Run it!
Great. Our endpoint is booted! Now let’s send a request!
Woo! We made a request from one Node process, through Remit, to another Node process and back again! As long as these processes are connected to Remit, they can find eachother and communicate!
Improvements
Right now, this returns a fixed piece of data, 'Hello, world!'
, but let’s change that to instead use the data we’ve been sent and say hello to a particular person. We’ll adjust our endpoint.js
file to use a function for its handler instead.
event
is a useful object. Most importantly, we can extract data sent by requests. Here, we’re grabbing thename
property from the incomingdata
and using it to form what we return.
Let’s change the request too so that we’re sending name
now.
Cool. We’ll give that a try:
Woohoo!
Next: Concepts