iot-messaging
v1.0.49
Published
Lynx iot-messaging
Downloads
31
Maintainers
Readme
Lynx Iot-messaging
Getting started
Installation
Install this library using npm as follows
npm i iot-messaging@latest
Example
import { ActiveMqMessagingClientFactory, AzureMessagingClientFactory, MessagingClientFactory, MessagingClientSender,MessagingMiddleware } from 'iot-messaging';
Object.assign(global, { WebSocket: require('ws') }); # You might need to run 'npm i ws'
const messagingProvider = MessagingMiddleware.ActiveMQ;
let client: MessagingClientFactory;
if (messagingProvider == MessagingMiddleware.ActiveMQ) {
client = new ActiveMqMessagingClientFactory('endpoint', 'username', 'password');
} else {
client = new AzureMessagingClientFactory('endpoint');
}
let senderClient: MessagingClientSender;
client.createClient((sender => {
senderClient = sender;
//! Create and run TCP server
})
);
Example of Sending Batch Messages
const messages = result.messages.map((message: any) => ({
body: message,
properties: {
Source: source,
Version: config.messaging.version,
},
sessionId: imei,
}));
this.senderClient.sendBatch(config.messaging.queueName, messages);
Example of Subscribing to a queue
if (this.messagingProvider == MessagingMiddleware.ActiveMQ) {
this.subClient = new ActiveMqSubscribingClientFactory(ApplicationConfiguration.activeMq.uri, ApplicationConfiguration.activeMq.username, ApplicationConfiguration.activeMq.password);
}
else {
this.subClient = new AzureSubscribingClientFactory(ApplicationConfiguration.azureServiceBus.connectionString);
}
this.subClient.createClient((receiver => {
this.receiverClient = receiver;
});
);
this.receiverClient.subscribeByQueueNameAndSessionId(ApplicationConfiguration.messaging.queueName, '4', (messages) => { console.log(messages) });
interface SubscribingClientReceiver {
subscribeByTopic(params: SubscribingParams): any;
subscribeByQueueName(queueName: string, processMessage: (message: ServiceBusReceivedMessage | IMessage[]) => void): any;
subscribeByQueueNameAndSessionId: (queueName: string, sessionID: string,
processMessage: (message: ServiceBusReceivedMessage | IMessage[]) => void) => void;
peekMessages: (queueName: string, sessionId: string, maxNumberOfMessages: number) => Promise<ServiceBusReceivedMessage[]>
}