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

firebase.io

v1.0.0

Published

A Firebase-based alternative to Socket.IO, providing real-time communication and event-driven functionality using Firebase Realtime Database without a server.

Downloads

81

Readme

FireSocket

FireSocket is a lightweight and simple WebSocket-like solution for Firebase Realtime Database, enabling real-time events, room management, and socket-like communication between clients using Firebase as the backend. This package is designed to mimic the behavior of Socket.IO but with Firebase.

Features

  • Real-time communication using Firebase Realtime Database
  • Socket-like communication between clients
  • Room-based communication and events
  • Map custom IDs to socket IDs
  • Handle connect and disconnect events
  • Emit and listen to custom events across rooms or specific clients

Installation

Install the required dependencies using npm:

npm install firebase.io

Then, import FireSocket into your project:

import FireSocket from "firebase.io";

Usage

Initialization

To create a new instance of FireSocket, provide your Firebase Realtime Database URL:

const fireSocket = new FireSocket("https://your-firebase-database-url");

Connecting to Firebase

FireSocket automatically connects to Firebase and creates a unique socket ID for each instance. You can retrieve the socket ID with:

console.log(fireSocket.id);
// 3d585b39-0b3e-496f-9d0b-eabc0ce37c36

Listening to Events

You can listen for custom events using the on method:

fireSocket.on("your-event", (data) => {
  console.log("Received data: ", data);
});

You can also handle the connection event:

fireSocket.on("connect", () => {
  console.log("Connected to Firebase!");
});

Emitting Events

To emit custom events:

fireSocket.emit("your-event", { key: "value" });

You can also emit events to specific rooms:

fireSocket.toRoom("room-name").emit("your-event", { key: "value" });

Rooms

Joining a Room

Join a room by calling the join method:

fireSocket.join("room-name");

Leaving a Room

To leave a room:

fireSocket.leave("room-name");

Custom ID Mapping

You can map custom user IDs to socket IDs. It is helpful to send events to single socket e.g. chat applications.

fireSocket.mapId("custom-id");

And send events to a specific user mapped by their ID:

fireSocket.toMapId("custom-id").emit("your-event", { key: "value" });

Disconnecting

To disconnect the socket manually:

fireSocket.disconnect();

Retrieving Sockets

You can retrieve the list of all connected sockets with:

fireSocket.getSockets((sockets) => {
  console.log(sockets);
});

Retrieving Rooms

You can get all rooms and their members with:

fireSocket.getRooms((rooms) => {
  console.log(rooms);
});

Leaving and Stopping Event Listeners

To stop listening to a specific event, use:

fireSocket.off("your-event");

Example

Here's a complete example of using FireSocket:

import FireSocket from "firebase.io";

const fireSocket = new FireSocket("https://your-firebase-database-url");

fireSocket.on("connect", () => {
  console.log("Connected to Firebase!");

  fireSocket.emit("greet", { message: "Hello, World!" });

  fireSocket.on("greet-back", (data) => {
    console.log("Received: ", data);
  });
});

fireSocket.join("chat-room");

fireSocket.toRoom("chat-room").emit("message", { text: "Hello everyone!" });

fireSocket.on("message", (data) => {
  console.log("New message in chat-room: ", data);
});

API

Methods

  • fireSocket.on(event: string, callback: (data: any) => void) - Listen for a specific event.
  • fireSocket.emit(event: string, data: any) - Emit an event to all clients.
  • fireSocket.toRoom(room: string).emit(event: string, data: any) - Emit an event to a specific room.
  • fireSocket.join(room: string) - Join a room.
  • fireSocket.leave(room: string) - Leave a room.
  • fireSocket.mapId(id: string) - Map a custom ID to a socket.
  • fireSocket.toMapId(id: string).emit(event: string, data: any) - Emit an event to a specific mapped user ID.
  • fireSocket.disconnect() - Manually disconnect the socket.

Properties

  • fireSocket.id - The unique ID of the socket.
  • fireSocket.connected - Connection status (boolean).

License

MIT License.