Giter VIP home page Giter VIP logo

br4ve / socket.io-event-retriever Goto Github PK

View Code? Open in Web Editor NEW
4.0 3.0 0.0 161 KB

socket.io-event-retriever is a helper built for and on top of socket.io to retrieve missed events by client. It can store the events emitted by server automatically and re-emits the ones the client has missed because of connection lost

JavaScript 100.00%
socket-io connection-lost retrieve-data socket-io-middleware

socket.io-event-retriever's Introduction

event-retriever-logo

socket.io-event-retriever

socket.io-event-retriever is a helper built for and on top of socket.io to retrieve missed events by client. It can store the events emitted by server automatically and re-emits the ones the client has missed because of connection lost.

  • It can store events based on time or event count
  • It can detect connection lost and use it for later retrieval
  • It can detect connection close (with socket.close() on client-side) and use it for later retrieval
  • It can send the missed events only to socket which were disconnected
  • It can auto delete events after certain amount of time to free up memory
  • It can be used with Redis

How does it work?

socket.io-event-retriever stores events which are sent from the server based on their room name. For example if the server emits an event called "Hello" to the room of "World", this event is saved into event-store so it can be used for later retrieval;

// server.js
io.to("World").emit("Hello");
// This event is saved into the event store under "World" room
io.to("My_World").emit("Hello");
// This event is saved into the event store under "My_World" room

Later, if one of the clients gets disconnected by various reasons and gets reconnected, event-retriever will check the rooms this client was joined and re-emits events which the client missed during disconnection.

Example Setup

Server Side

const express = require("express");
const http = require("http");
const socketIO = require("socket.io");
const { EventRetrieverIO } = require("../lib");

const app = express();
const server = http.createServer(app);
const io = socketIO(server);
// "options" can be provided to EventRetrieverIO as a second argument
const ioEventRetriever = new EventRetrieverIO(io);

io.on("connection", (socket) => {
  // put method to the top of the scope
  ioEventRetriever.setSocket(socket);

  socket.join("some_room");
  socket.on("emitted_event", (data) => {
    io.to("some_room").emit("emitted_event");
  });
});

server.listen(5000, () => {
  console.log("connected to server");
});

Client Side

const { EventRetrieverClient } = require("../lib");
const io = require("socket.io-client");
// initialize io
const socket = io("some.url");
// call eventRetriverClient with Socket
const socketEventRetriever = new EventRetrieverClient(socket);
// init retriever
socketEventRetriever.init();

API

  • EventRetrieverIO
  • EventRetrieverClient

EventRetrieverIO(io, options)

Accepts the top level io object (comes from socket.io), optional options argument and returns a retriever instance.

This method creates a storage to store events emitted from server side. Storage options can be changed by providing a options argument.

The parameters of default options object are listed below:

Property Description Default
stackCheck If it is true, stores the events like a stack and removes events one by one after the stackCheckLimit is exceeded true
stackCheckLimit The maximum count of stored events per room, it will be checked if stackCheck is enabled, for example if stackCheck is enabled it will store only the last 100 events emitted by server per room and when it becomes 101 it will delete the first event in that room 500
timeCheck If it is true, stores events based on time false
timeCheckLimit Miliseconds for timeCheck, which means for example if timeCheck is true and timeCheckLimit is 1000 * 10, events which are older than (now - timeCheckLimit) will be removed 1000 _ 60 _ 10
cronOptions An object which defines how frequently the rooms will be checked and after how many minutes of inactivity the rooms will be removed { checkEvery: "* _/10 _ * * _", removeAfter: 1000 _ 60 * 10}

.setEventStore(storeName, storeConfig)

With .setEventStore(), default store which is used store events can be changed. By default socket.io-event-retriever uses local memory of Node.js process. If your services are served from multiple instances you can change default store to be consistent across different Node.js processes.

Currently socket.io-event-retirever only supports Redis but in the future it will support other in-memory databases.

const ioEventRetriever = new EventRetrieverIO(io);
// set redis as event store
ioEventRetriever.setEventStore("redis", { host: "localhost", port: 6379 });
// set to default event store
ioEventRetriever.setEventStore("base");

Todos

  • Detect page reload on the front-end to retrieve events missed while page reload
  • Write tests
  • Release the library on npm
  • Support other in-memory databases

Contributers & Supporters

Special thanks to all contributers & supporters who helped me to release this library


Mert Batmazoğlu
Github

Arda Zafer İbin
Github

Questions & Feedback

I would like to answer any questions raised based on this project and I would be so glad if you provide me any positive or negative feedback. You can contact me at [email protected]

License

MIT

socket.io-event-retriever's People

Contributors

br4ve avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.