1.1.0 • Published 7 years ago
remote-ee v1.1.0
Remote EE
Remote event emitter is a lightweight way to use event emitters across a network.
Instalation
yarn add remote-ee
Usage
Remote ee is designed to be used a cross a server and one or more clients
Server.js
const { Server } = require("remote-ee");
const server = new Server();
server.listen(1337);
server.subscribe((ee, socket) => {
socket.send("message", "Hello from the server");
ee.on("message", message => {
console.log(message);
socket.send("leave now");
});
ee.on("quit", () => {
socket.send("message", "bye");
});
});
Client.js
const { Client } = require("remote-ee");
const client = new Client();
client.connect({ port: 1337 });
client.on("connect", () => {
client.send("message", "Hello from the client");
});
client.subscribe(ee => {
ee.on("message", message => {
console.log(message);
});
ee.on("leave now", () => {
client.send("quit");
});
});
Api
Server
Server is a subclass of net.Server
Server#subscribe(ee: EventEmitter, socket: net.Socket)
Setup a subscribe handler which will be called on every connection to the server. The socket has an added send property.
Client
Client is a subclass of net.Client
Client#subscribe(ee: EventEmitter)
Subscribe to the client connection. This handler will be called once.
Client#send(type: string, payload: any)
Send an event to the server, the payload must be json sterilizable.