4.1.8 • Published 6 years ago

rabbit-communication-adapter v4.1.8

Weekly downloads
5
License
ISC
Repository
-
Last release
6 years ago

rabbit-communication-adapter

Usage

npm init

npm install git+https://github.com/GamingBattleGround/rabbit-communication-adapter.git

To use it in a gbg microservice, you will need to add env variables,

RABBIT_URI=***************
REDIS_URI=**************
ENV=dev
MICROSERVICE=chat/notifications...

and use them like this:

//use case: echo the message to the user

require('dotenv').load();
var Rabbit = require('rabbit-communication-adapter')
var rabbit = new Rabbit(process.env.RABBIT_URI, "microserviceQueueName", peers)

//gateway forwarded the users request, here you can catch the event
rabbit.emitter.on('event from user',(payload, userId)=>{
//do something with data
})

//this is how we send the message, where "ResEvent" is the event name that user is listening to   
   rabbit.sendToUser("event from ms", payload, userId)

To test a simple user behaviour, you can start a client:

//connectiong to our gateway
var io_client = require('socket.io-client')
var client = io_client.connect('https://gbg-gateway-dev.herokuapp.com')

//client logs in
client.emit('user logged in', 47)

//client emits event
setTimeout(()=>{
    client.emit('chat', 'event from user', 'payload', 47)
},1500)

//here client receives an event from microservice
client.on('event from ms',(payload) => {
    console.log("echo: " + payload)
})

If you want to send a message to a microservice with a delay, you should add array of peers as the third parameter to the constructor (in this case, the receiver and sender are the same microservice):

require('dotenv').load();
var Rabbit = require('rabbit-communication-adapter')
var rabbit = new Rabbit(process.env.RABBIT_URI, "matchlobby", ["matchlobby"])
//setTimeout is there to let the constructor finish its work before trying to use methods defined in a class
setTimeout(()=>{
    rabbit.sendToMicroservice("matchlobby", "hello", "payload", 10000)    
},3000)

rabbit.emitter.on('hello', (payload)=>{
   console.log("event from ms", payload)
})

Notes

For simulating client, you will need to install socket.io. Be careful not to commit it since it's dev dependency only.

Logging

If you want to log incomming and outgoing messages in a microservice, add DEBUG = true to .env Also note that log output is tagged. Rx is received message, TxU is transmitting to user and TxM is transmitting to a microservice

4.1.8

6 years ago

4.1.7

6 years ago

4.1.6

6 years ago

4.1.5

6 years ago

4.1.4

6 years ago

4.1.3

6 years ago

4.1.2

6 years ago

4.1.1

6 years ago