Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Relay messages from RabbitMQ to a browser using Socket.io
<!DOCTYPE html>
<html>
<head>
<script src="/socket.io/socket.io.js"></script>
<script>
(function () {
var onMessage = function (data) {
// Do something with the message data
};
var connectToServer = function () {
var socket = io.connect('http://localhost:8080');
socket.on('message-name', onMessage);
};
connectToServer();
})();
</script>
</head>
<body>
</body>
</html>
var amqp = require('amqp'),
express = require('express'),
app = express.createServer(),
io = require('socket.io').listen(app),
rabbitMq = amqp.createConnection({ host: 'rabbitmq-host.com' });
app.configure(function () {
app.use(express.static(__dirname + '/public'));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
rabbitMq.on('ready', function () {
io.sockets.on('connection', function (socket) {
var queue = rabbitMq.queue('my-queue');
queue.bind('#'); // all messages
queue.subscribe(function (message) {
socket.emit('message-name', message);
});
});
});
app.listen(8080);
@hlmn

This comment has been minimized.

Copy link

commented Mar 21, 2018

thanks

@memon07

This comment has been minimized.

Copy link

commented Jun 22, 2018

great! thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.