1

Now my project use Django as API and NodeJs (SocketIO) as server which require Realtime for pushing Notifications

I try pushing Notifications to Node.js Socket.io using Redis PubSub but not success. Please check out my code error:

My Python code. I publish to myChannel sample message:

def test_vew(request):
    REDIS_SERVER = redis.Redis(host='localhost', port=6379, db=0)
    REDIS_SERVER.publish("myChannel", "Demo Message")
    return Response({ "success": True }, status=200)

My NodeJS Code:

var app = require('http').createServer()
const PORT = process.env.PORT || 3000;
var redis = require('redis').createClient();
const server = app.listen(3000, () => console.log('listening on port 3000'))
//sockets
const io = require('socket.io').listen(app)

redis.subscribe('myChannel', (message) => {
  console.log(`Got message ` + message)
})
io.sockets.on("connection", function(socket) {
  console.log("A User Connected to SocketIO");
  redis.on("pmessage", function(pattern, channel, message) {
    console.log(channel, message);
  });
});

When I run function in Django, My NodeJS Socket Subcribe can't grab message (Nothing log in console). I don't know the reason.

2
  • 1
    Did you select the same redis database?
    – Dmitrii G.
    Commented Jun 11, 2019 at 4:24
  • @DmitriiG.You mean select in NodeJS?
    – KitKit
    Commented Jun 11, 2019 at 4:27

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.