npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cube-socket-client

v1.0.8

Published

A lightweight TypeScript wrapper for `socket.io-client` that handles socket connections, heartbeat rooms, broadcasting, and topic subscriptions, designed for efficient real-time communication in modern applications.

Downloads

4

Readme

Cube Socket Client

A lightweight TypeScript wrapper for socket.io-client that handles socket connections, heartbeat rooms, broadcasting, and topic subscriptions, designed for efficient real-time communication in modern applications.

Features

  • Simplified integration with socket.io-client.
  • Heartbeat mechanism for maintaining active connections.
  • Ability to differentiate between listening clients and broadcasting clients.
  • Create, join, and leave rooms for organized message handling.
  • Subscribe and unsubscribe to topics dynamically.
  • Broadcast messages to rooms or topics.
  • TypeScript support with built-in type declarations.

Installation

You can install the package via npm:

npm install cube-socket-client

Usage

Here’s how you can use Cube Socket Client in your project:

Basic Setup

import GlobalSocketClient from "cube-socket-client";

// Initialize the client
const client = new GlobalSocketClient(
  "your-api-key",
  "https://your-socket-server.com"
);
client.init();

// Subscribe to a topic
client.subscribe("some-topic", (message) => {
  console.log("Received message on some-topic:", message);
});

// Broadcast a message to a topic or room
client.broadcast("some-topic", { data: "Hello World!" });

// Check if connected
if (client.isConnected()) {
  console.log("Successfully connected to the socket server");
}

// Close the socket connection when done
client.close();

Heartbeat Mechanism

Upon connecting, the client automatically joins a "heart-beat" room and broadcasts service information at regular intervals (every 60 seconds) to ensure the connection remains active.

API Documentation

Constructor

constructor(apiKey: string, serverUrl: string, shouldBroadcast = true)

  • apiKey: A unique key that identifies your service.
  • serverUrl: The URL of the socket server.
  • shouldBroadcast: Optional boolean to indicate if the client should broadcast its own heartbeat messages (default: true).

Methods

init()

Initializes the socket connection.

subscribe(topic: string, callback: (message: any) => void)

Subscribes to a topic and triggers the callback whenever a message is received on that topic.

  • topic: The name of the topic to subscribe to.
  • callback: A function to be called with the message received on the topic.

unsubscribe(topic: string)

Unsubscribes from a specific topic.

  • topic: The name of the topic to unsubscribe from.

createRoom(room: string)

Creates a room and joins it.

  • room: The name of the room to create.

leaveRoom(room: string)

Leaves a specific room.

  • room: The name of the room to leave.

createTopic(topic: string)

Creates a new topic (this only affects local management).

  • topic: The name of the topic to create.

broadcast(topicOrRoom: string, message: any)

Broadcasts a message to a specific topic or room.

  • topicOrRoom: The name of the topic or room where the message should be sent.
  • message: The message content to send.

isConnected(): boolean

Returns true if the socket is connected to the server, otherwise false.

close()

Closes the socket connection and clears any active intervals (such as heartbeat).

Example: Heartbeat Room

The client automatically joins a heart-beat room to send periodic heartbeat messages containing service information. This is done to maintain an active connection with the server:

const serviceInfo = {
  serviceName: "your-api-key",
  timestamp: new Date().toISOString(),
};

// Heartbeat message will be broadcast every 60 seconds
client.broadcast("heart-beat", serviceInfo);

License

This project is licensed under the MIT License - see the LICENSE file for details.

Author

Developed by Platform Team. You can reach out for support or contributions via [email protected].