Skip to content

Instantly share code, notes, and snippets.

@oldmud0
Created July 8, 2018 04:08
Show Gist options
  • Save oldmud0/bf78f27525f05faa07b8a9ef981f1f03 to your computer and use it in GitHub Desktop.
Save oldmud0/bf78f27525f05faa07b8a9ef981f1f03 to your computer and use it in GitHub Desktop.
node-ipc example with high client->server reliability (no dropped messages)
const ipc = require("node-ipc");
const async = require("async");
const queue = async.queue((data, cb) => {
// There is no way of checking if client is connected so just
// catch and put it back in the queue if a bad happens.
try {
ipc.of.test.emit("message", data);
} catch (err) {
console.error(err);
queue.unshift(data);
}
cb();
});
queue.pause();
let i = 0;
ipc.config.id = "test";
ipc.config.silent = true;
setInterval(() => {
console.log(`Buffered message ${i}`);
queue.push(i++);
}, 500);
ipc.connectToNet("test", () => {
ipc.of.test.on("connect", () => {
console.log("connected");
queue.resume();
});
ipc.of.test.on("message", (data) => {
console.log(data);
});
ipc.of.test.on("error", (err) => {
queue.pause();
})
});
const ipc = require("node-ipc");
ipc.config.id = "test";
ipc.serveNet(() => {
ipc.server.on("connect", () => {
ipc.server.broadcast("message", "yoyooyoyoy");
});
ipc.server.on("message", (data, socket) => {
ipc.server.emit(socket, "message", data);
});
});
ipc.server.start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment