moscajs / mosca

MQTT broker as a module
mosca.io
3.2k stars 513 forks source link

load banlancer #719

Closed willin closed 6 years ago

willin commented 6 years ago

i have several servers

their config file are the same, all connect to a same redis server:

const moscaSettings = {
  host: 'same_server_ip'
  port: 1883,
  backend: {
    host: 'same_server_ip'
  },
  persistence: {
    factory: mosca.persistence.Redis,
    host: 'same_server_ip'
  }
};

and what the mosca server do is receive messages and insert them to mysql server.

the situation now is: when a message was sent to a specific/single server, all servers get the message and then this message was inserted duplicate times.

mcollina commented 6 years ago

How do you do this insertion? the published event should happen only in the originating server.

willin commented 6 years ago
const messageHandler = async (topic, payload) => {
  const decodedMessage = Message.decode(payload);
  await mysqlInsert(decodedMessage);
};

server.subscribe('device/+/pubmessage', messageHandler);
mcollina commented 6 years ago

You should use the published event then, not subscribing. That is equivalent to a client subscription.

willin commented 6 years ago
server.on('published', (packet, client) => {

});

do you mean this?

we tried this before, but it receives too many unuseful sys messages.

mcollina commented 6 years ago

That’s what you should use. You can filter using mqemitter if you need to.

willin commented 6 years ago

oic