A Nest microservice transporter for Solace PubSub+
To begin using it, we first install the required dependency.
$ npm install --save nest-solace-pubsub-transporter solclientjs
To use the Solace PubSub+ transporter, pass the following options object with a strategy
property using SolacePubSubServer
to the createMicroservice()
method:
const app = await NestFactory.createMicroservice<MicroserviceOptions>(
AppModule,
{
strategy: new SolacePubSubServer({
url: 'tcp://localhost:55554',
vpnName: 'default',
userName: 'admin',
password: 'admin',
}),
},
);
The SolacePubSubServer
constructor options object is based on solace.SessionProperties
. The Solace PubSub+ transporter exposes the properties described here.
Create an instance of SolacePubSubClient
class and run the send()
method, subscribing to the returned observable stream.
const client = new SolacePubSubClient({
url: 'tcp://localhost:55554',
vpnName: 'default',
userName: 'admin',
password: 'admin',
});
client
.send('pattern', 'Hello world!')
.subscribe((response) => console.log(response));
To dispatch an event (instead of sending a message), use the emit()
method:
solacePubSubClient.emit('event', 'Hello world!');
In more sophisticated scenarios, you may want to access more information about the incoming request. When using the Solace PubSub+ transporter, you can access the SolacePubSubContext
object.
@MessagePattern('notifications')
getNotifications(@Payload() data: number[], @Ctx() context: SolacePubSubContext) {
console.log(`Message: ${context.getMessage()}`);
}
A working example is available here.