0

Hello guys i'm having a problem in redis pub/sub because my client does not show the message i've published in redis-cli. I used the codes found here in stackoverflow and i made some modification. Here is the link and code. I hope you can help me, my goal is to publish the message to the client index.html using redis publish in redis-cli. I've done this before but i can't make it work again. Thanks in advance guys.

Here is my client index.html

<html>
<head>
    <title>PubSub</title>
    <script src="/socket.io/socket.io.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
    <!-- <script src="/javascripts/socket.js"></script> -->
</head>
<body>
    <div id="content"></div>
    <script>
            var socket = io.connect('http://localhost:3000');    
            var content = $('#content');

            socket.on('connect', function() {
            });

            socket.on('message', function (message){
                content.prepend(message + '<br />');
            }) ;

            socket.on('disconnect', function() {
                console.log('disconnected');
                content.html("<b>Disconnected!</b>");
            });

            socket.connect();
        });
    </script>
</body>
</html>

Here is my server.js

var express = require('express');
var app = express();
var redis = require('redis');
var http = require('http');
var server = http.createServer(app);
var socket = require('socket.io').listen(server);
var publish = redis.createClient();

app.listen(3000);
    console.log("Express server listening on port 3000")

    app.get('/', function (req,res) {
        res.sendfile(__dirname + '/public/index.html');
    });

socket.on('connection', function (client) {
        var subscribe = redis.createClient();
        subscribe.subscribe('pubsub');

        subscribe.on("message", function (channel, message) {
            publish.send(message);
        });

        publish.on('message', function (msg) {
        });

        publish.on('disconnect', function() {
            subscribe.quit();
        });
    });
Community
  • 1
  • 1
Joenel de Asis
  • 1,401
  • 3
  • 15
  • 14

1 Answers1

1

Redis will not send data to connected clients for you. You must instruct Socket.IO to emit data:

subscribe.on("message", function (channel, message) {
  socket.emit('message', message);
});
hexacyanide
  • 88,222
  • 31
  • 159
  • 162