SimpleExternalRabbitMQ
This server-side API class provides methods to generate and send messages to an external queue based on RabbitMQ.
getErrors()
Use this method to return error messages if an error occurred while sending the messages.
Return:
| Type | Description |
|---|---|
| Array of Strings | This method returns the error values. |
| Type | Description |
|---|---|
| Array of Strings | This method returns the error values. |
Example:
const rabbitMq = new SimpleExternalRabbitMQ('notification-service');
const payload = {
userIDs: [2344, 7444],
message: "Hello!"
}
ss.info(rabbitMq.publishMessage('push-notifications', JSON.stringify(payload), {ContentType: "application/json", Headers: {"X-Source": "simple"}}).getErrors());
publishMessage(routingKey, payload, options)
Use this method to send a message to a selected queue.
The maximum message length is 26000 characters.
Parameter(s):
| Name | Type | Mandatory | Default value |
|---|---|---|---|
| routingKey | String | Y | N |
| payload | String | Y | N |
| options | Object | N | N |
Set of fields for options parameter
| Field | Type | Value |
|---|---|---|
| UserId | String | not set |
| Type | String | not set |
| Timestamp | String | 0001-01-01 00:00:00 |
| ReplyTo | String | not set |
| Priority | Integer | 0 |
| MessageId | String | not set |
| Mandatory | Boolean | false |
| Immediate | Boolean | false |
| Headers | Object | {} |
| Expiration | String | not set |
| Exchange | String | not set |
| DeliveryMode | Integer | 1 |
| CorrelationId | String | not set |
| ContentType | String | text/plain |
| ContentEncoding | String | not set |
| AppId | String | not set |
Return:
| Type | Description |
|---|---|
| SimpleExternalRabbitMQ | This method returns an instance on which it was called. |
Example:
const rabbitMq = new SimpleExternalRabbitMQ('nofication-service');
const payload = {
userIDs: [2344, 7444],
message: "Hello!"
}
ss.info(rabbitMq.publishMessage('push-notifications', JSON.stringify(payload), {ContentType: "application/json", Headers: {"X-Source": "simple"}}).getErrors());
publishMultipleMessages(routingKey, payload, options)
Use this method to send an array of messages to a selected queue.
The maximum message length is 26000 characters.
Parameter(s):
| Name | Type | Mandatory | Default value |
|---|---|---|---|
| routingKey | String | Y | N |
| payloads | Array of Strings | Y | N |
| options | Object | N | N |
Set of fields for options parameter
| Field | Type | Value |
|---|---|---|
| UserId | String | not set |
| Type | String | not set |
| Timestamp | String | 0001-01-01 00:00:00 |
| ReplyTo | String | not set |
| Priority | Integer | 0 |
| MessageId | String | not set |
| Mandatory | Boolean | false |
| Immediate | Boolean | false |
| Headers | Object | {} |
| Expiration | String | not set |
| Exchange | String | not set |
| DeliveryMode | Integer | 1 |
| CorrelationId | String | not set |
| ContentType | String | text/plain |
| ContentEncoding | String | not set |
| AppId | String | not set |
Return:
| Type | Description |
|---|---|
| SimpleExternalRabbitMQ | This method returns an instance on which it was called. |
Example:
const rabbitMq = new SimpleExternalRabbitMQ('notification-service');
const messages = [
{
userID: 2344,
message: "Hello, Ivan"
},
{
userID: 7444,
message: "Hello, Petr"
}
];
rabbitMq.publishMultipleMessages(
'push-notifications',
messages.map(m => JSON.stringify(m)),
{
ContentType: "application/json",
Headers: {
"X-Source": "simple"
}
}
)
ss.info(rabbitMq.getErrors());
testConsumerConnection(id, options)
Use this method to create a test RabbitMQ consumer connection that will be closed after testing. The method is static, so you do not need to create a new SimpleExternalRabbitMQ object, you can call the method directly from the class as shown in the example below.
Parameter(s):
| Name | Type | Mandatory | Default value |
|---|---|---|---|
| id | String | N | N |
| options | Object | N | N |
Set of fields for options parameter
| Field | Type | Value |
|---|---|---|
| host | String | not set |
| port | Integer | not set |
| username | String | not set |
| password | String | not set |
| vhost | String | not set |
Return:
| Type | Description |
|---|---|
| String | The method returns the text of the error thrown as a result of the test connection, or a success message. |
Example:
let err = SimpleExternalRabbitMQ.testConsumerConnection(123123123, {username: "newlogin"})
ss.info(err ? err : "success!")
testProducerConnection(id, options)
Use this method to create a test RabbitMQ producer connection that will be closed after testing. The method is static, so you do not need to create a new SimpleExternalRabbitMQ object, you can call the method directly from the class as shown in the example below.
Parameter(s):
| Name | Type | Mandatory | Default value |
|---|---|---|---|
| id | String | N | N |
| options | Object | N | N |
Set of fields for options parameter
| Field | Type | Value |
|---|---|---|
| host | String | not set |
| port | Integer | not set |
| username | String | not set |
| password | String | not set |
| vhost | String | not set |
Return:
| Type | Description |
|---|---|
| String | The method returns the text of the error thrown as a result of the test connection, or a success message. |
Example:
let err = SimpleExternalRabbitMQ.testProducerConnection(123123123, {username: "newlogin"})
ss.info(err ? err : "success!")