Created
August 28, 2017 22:36
-
-
Save kiramishima/07bb633706d5cf72afde54479f8806a4 to your computer and use it in GitHub Desktop.
RMQ + Socket.io
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as SocketIO from 'socket.io'; | |
import * as socketioJwt from "socketio-jwt"; | |
import amqp from 'amqplib/callback_api'; | |
/** Start */ | |
export function startSocketServer(server) { | |
// Create SocketIO Listener | |
const io = SocketIO.listen(server); | |
// RabbitMQ Consumer | |
amqp.connect(process.env.RMQ_HOST, function(err, conn) { | |
conn.createChannel(function(err, ch) { | |
var ex = 'Orders'; | |
ch.assertExchange(ex, 'fanout', {durable: false}); | |
ch.assertQueue('', {exclusive: true}, function(err, q) { | |
console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", q.queue); | |
ch.bindQueue(q.queue, ex, ''); | |
ch.consume(q.queue, function(msg) { | |
log.info(" [x] Received %s", msg.content.toString()); | |
var data = JSON.parse(msg.content.toString()); | |
io.to(data.channel).emit('new-order', data.order_data); | |
}, {noAck: true}); | |
}); | |
}); | |
}); | |
// SocketIO Handlers | |
io.sockets.on('connection', function (socket) {}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment