socket.io-client-once
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

Socket.io Client Once

Socket.io Client once is a very simple package that allows you to avoid the multiple event listening issues associated with socket.io client library.

This simple library prevents multiple creation of socket listeners that grossly affect performance with SAP applications.

Creating socket client instance

This library similar interface with the traditional socket.io library, so it can be easily replaced any existing socket.io-client implementation, or so I thought.

import { io } from "socket.io-client-once";

const client = io("socket.io-url", {
  // accept all socket.io client options
  transports: ["websocket"],
  autoConnect: true,
});

client.on("event-to-listen-to", (data: any) => {
  // process  response data
  console.log(data);
});

client.emit("event-to-emit", {
  // event data
});

Event listener order

When you add multiple event listener of the same event, only the last listener will be processed. see the example below:

/**
 * Despite adding multiple event listeners for the same event, only the last listener added
 * will be processed.
 *
 * @note this allows us to avoid multiple instance listener scenario synonymous with
 * socket.io client libraries. So our listener will only fire once
 **/

client.on("event-to-listen-to", (data: any) => {
  // This listener will not be processed
});
client.on("event-to-listen-to", (data: any) => {
  // Only the last listener will be processed
});

Getting the underlining socket.io client

To get the underlining socket.io client instance, you can use the following. This will enable you to perform any other task related traditional socket.io client

License

MIT

Package Sidebar

Install

npm i socket.io-client-once

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

12.3 kB

Total Files

7

Last publish

Collaborators

  • maccabeus