HermesJS
Build message-driven APIs with ease.
Install
npm install hermesjs
Example
Create a simple app to receive messages from a MQTT broker:
const Hermes = ;const MqttAdapter = ; const app = ; app; app; app; app;
API
App
Constructor
const Hermes = ;const app = ;
app.addAdapter(Adapter, options)
Adds a connection adapter. Adapters are built independently, as Node.js packages.
For instance, using a MQTT example:
const MqttAdapter = ; app;
app.use(...fn)
app.use(HermesRouter)
app.use(route, ...fn)
app.use(route, HermesRouter)
Use middlewares and routes. If you know how Connect/Express works, it's exactly the same, but instead of getting req
and res
, you get a message
object.
Middlewares:
app; app;
Routes:
app;
HermesRouter
index.js
const hello = ; app;
routes/hello.js
const Router = ;const router = ; router; router; moduleexports = router;
Catch Errors
app;
app.useOutbound(...fn)
app.useOutbound(HermesRouter)
app.useOutbound(route, ...fn)
app.useOutbound(route, HermesRouter)
This is the same as app.use
but for the outbound communication. The middlewares you specify here will be used before sending a message to the server or broker.
app;
app.send(payload, headers, topic)
app.send(HermesMessage)
It sends a message to the server. The message will go through all the outbound middlewares before it reaches the adapters.
app;
app.connect()
Starts the application and connects to the server using the adapters.
app;
You can also use the
app.listen()
alias.
Message
message.reply(payload, headers, topic)
Replies back to the server.
app;
message.params
Object containing all the params in the message topic, i.e.:
appinbroker;
Adapters
- MQTT adapter: https://github.com/hitchhq/hermes-mqtt
- AMQP adapter: https://github.com/hitchhq/hermes-amqp
- Socket.IO adapter: https://github.com/hitchhq/hermes-socketio
Author
Fran Méndez (fmvilas.com)