Multiple instances of simple chat with Node.js, Websocket - Socket.IO and Redis -
i trying create simple chat app node.js , websocket using socket.io. inspired myself german tutorial , single backend instance works well.
i have 2 instances of server in backend, should sync chat messages between each other , store chat history in redis. client should display last 10 messages current channel, when new chat-room joined.
i tried apply solution stackoverflow page issues. first
websocket connection 'ws://localhost:8080/socket.io/?eio=3&transport=websocket&sid=leqgvy0cvk9yaszbaaaa' failed: connection closed before receiving handshake response
error in console. , second, both clients receiving 'payload' messages in 1-sec interval. not understand how redis-sync mechanism works , not yet know how display chat history.
here code far:
var conf = require('./config.json'); var cluster = require('cluster'); var os = require('os'); if (cluster.ismaster) { // create http server, not use listen // way, have socket.io server doesn't accept connections var server = require('http').createserver(); var io = require('socket.io').listen(server); var redis = require('socket.io-redis'); io.adapter(redis({ host: conf.redisuri, port: conf.redisport })); setinterval(function() { // workers receive in redis, , emit io.emit('chat', {time: new date(), text:'payload'}); }, conf.syncintervall); // set number of workers (var = 0; < conf.numberofworkers; i++) { cluster.fork(); } cluster.on('exit', function(worker, code, signal) { console.log('worker ' + worker.process.pid + ' died'); }); } if (cluster.isworker) { var express = require('express'); var app = express(); var http = require('http'); var server = http.createserver(app); var io = require('socket.io').listen(server); var redis = require('socket.io-redis'); // webserver //app.listen(conf.defaultport); server.listen(conf.defaultport); // deliver static files app.use(express.static(__dirname + '/public')); // route / path app.get('/', function (req, res) { // send index.html in reponse res.sendfile(__dirname + '/public/index.html'); }); // websocket io.adapter(redis({ host: conf.redisuri, port: conf.redisport })); // callback when client connects io.sockets.on('connection', function (socket) { // store room name in socket session client socket.room = 'defaultchannel'; // send client room 1 socket.join('defaultchannel'); // echo client they've connected socket.emit('chat', { time: new date(), text: 'you have connected room defaultchannel on server!' }); socket.emit('chat', { time: new date(), text: 'connected chat worker-node: ' + cluster.worker.id}); // if message received socket.on('chat', function (data) { // send other clients console.log('the current channel', socket.room); socket.broadcast.in(socket.room).emit('chat', { time: new date(), name: data.name || 'anonymous', text: data.text }); }); // if client joins channel socket.on('join', function (room) { // store room name in socket session client socket.room = room; // send client room 1 socket.join(room); console.log('client joined room ' + room); }); }); // log port number in console console.log('server running under http://127.0.0.1:' + conf.defaultport + '/'); }
Comments
Post a Comment