Echoes
Echoes is an abstraction of kafkajs. It can be used as a standalone package without installing other orionjs packages.
Install package
- npm
- Yarn
npm install @orion-js/echoes
yarn add @orion-js/echoes
startService
See Client configuration for client params. See Consuming messages for consumer params. See Producing messages for producer params.
echoes
is an object containing all of the service echo
import {startService} from '@orion-js/echoes'
import echoes from 'app/components/echoes'
import {route} from '@orion-js/app'
startService({
client: {
clientId: 'appName',
brokers: ['localhost:9092']
},
consumer: {
groupId: 'microserviceId'
},
producer: {},
requests: {
key: 'secretPassword',
startHandler: handler => route('/echoes-services', handler),
services: {
example: 'http://localhost:4100/echoes-services'
}
},
echoes
})
echo
An echo
is a message handler. You can define multiple echoes in your app. All must be passed in the echoes
param of startService
. The key in the echoes
params will be the name of the topic
. An echo
definition must have 2 params. type
and resolve
:
Type can be echo.types.event
or echo.types.request
.
You must pass a resolve
function, which has 2 arguments.
params
: The params passed when this event is calledcontext
: Contains the arguments in theeachMessage
Kafkajs function
import {echo} from '@orion-js/echoes'
export default echo({
type: echo.types.event,
async resolve(params, context) {
console.log('Received an event', params)
}
})
publish
Publish sends a message that doesn't expects a response. It expects the following params:
topic
: Topic (echo
) to send the message toparams
: The params that theecho
will receive. This params are serialized usingserialize-javascript
so you can pass all JavaScript basic types (not including functions).ack
: See Producing acktimeout
: See Producing timeout
import {publish} from '@orion-js/echoes'
await publish({
topic: 'onEvent',
params: {
hello: 'world',
date: new Date(),
number: 134433,
bool: true,
regexp: /alsothis/gi
}
})
request
Publish sends a http request that expects an instant response. It expects the following params:
method
: Topic (echo
) to send the message toservice
: Service name that has an url mapped on the startService function.params
: The params that theecho
will receive. This params are serialized usingserialize-javascript
so you can pass all JavaScript basic types (not including functions).
import {request} from '@orion-js/echoes'
await request({
service: 'example',
method: 'getResponse',
params: {
hello: 'world',
date: new Date(),
number: 134433,
bool: true,
regexp: /alsothis/gi
}
})