node.js - Using (socket.io + RedisStore) to communicate across multiple servers -


I am working on a multiplayer online game, using Node.js and Socket.io. I hope many players are expected to join the game, so I'm hosting it on Amazon Opports.

The problem is that the servers are not able to send socket events to customers connected to a different server. I am using RedisStore to manage socket.io sessions. I believed that RedisStore and socket.io took care of this inter-server communication under the hood in an intuitive way, here another reference is given to the question:

But this is not the case that is so looks like. Messages do not go through other clients if they are on different servers; The app only works on one server but fails, if I am using loader multiple servers using ELB at oopsworks.

This is just a quote from the whole code. Please ignore syntax errors etc. If any

app.js
  // redis client initial time redis = require ("redis"); Redis_client = Required ('redis-url'). Connect ('redis: // xxxxx'); // settings for socket.io var RedisStore = requires ('socket.io/lib/stores/redis'), redis = is required ('socket.io/node_modules/redis'), pub = Redis.createClient (11042, '' ----- ''), sub = redis.createClient (11042, '-----'), client = redis.createClient (11042, '-----'); // RedisStore combo in combo with Socket.io to allow communication in multiple servers io.set ('store', new redisstore ({redis: redis, redispub: pub, redisSub: sub, redisClient: client}) Use; // Socket Communications specific code io.of ('/ game') .on ('connection', function (socket) {socket.on ('init', function) {var user_id = data.user_id ; // Collection User_id which was sent by the customer socket_id = socket.id; redis_client.set ("user_socket:" + user_id, socket_id, function (mistake, answer) {// has been stored that socket id for that user To red Is stored in the database}};}); Socket.on ('send_message', function (data) {var sender = data.sender_id; var reciepient = data.reciepient_id; // Message for user who redis_client.get ("User_socket:" + reciipient, function (err, socket_id) {if (socket_id) {var socket = io.of ('/ game') Sockets [socket_id]; socket.emit ("message", { Sender: sender}); // This fails. Other servers do not have to go through messages.}})})});    

You can not broadcast directly on other servers on socket objects Allow other servers to broadcast on 'Rooms', thanks to the socket.o 1.x, the new connections are automatically added to a room by the name of their socket id. To resolve your problem, change:

  if (socket_id) {var socket = io.of ('/ game'). Socket [socket_id]; Socket.emit ("Message", {Sender: Sender}); // This fails to send messages through the message server for others}   

To throw in the room instead of emitting on the socket object:

 < Code> if (socket_id) {io.to (socket_id) .emit ("message", {sender: sender}); // It does not go through messaging server for others fails}}   

And you may have more luck.

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -