I'm new to using soket.io and I'm learning how to create a server, but I'm not able to print the message, can someone help me?
server.js
var fs = require('fs')
var http = require('http').createServer(handler)
var io = require('socket.io')(http)
var Redis = require('ioredis')
var redis = new Redis(6379, '127.0.0.1')
redis.psubscribe('*', function(){
redis.on('pmessage', function(pattern, channel, message){
console.log(channel,message)
})
})
http.listen(3001, function(){
console.log('ouvindo porta 3001');
/*console.log(__dirname);*/
})
function handler(req, res) {
fs.readFile('http://localhost/123/index.html',
function (err, data){
if (err){
res.writeHead(500);
return res.end('erro ao carregar index.html')
}
res.writeHead(200);
res.end(data);
}
)
}
indesx.html:
<html>
<head>
<title>Exemplo Socket.io</title>
</head>
<body>
<h1>Simulando um cliente em nosso servidor</h1>
<div id="menssage"></div>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.1.1/socket.io.js"></script><script>varsocket=io('http://localhost:3001')socket.on('products',function(data){varel=document.querySelector('#menssage')el.innerHTML="<h1>" + data + "</h1>"
console.log(data)
})
</script>
</html>
It connects to the server created when I call it
ButwhenIsendamessage,IonlyseewhereI'meditingthecodeanditappearsinbrowers:
Butit'snotappearinginthebrowser