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

gsockets

v0.4.1

Published

Series of generic socket events

Downloads

22

Readme

Generic Socket Events

A set of generic events for sending and receiving json objects through socket.io.

Allows you to easily announce, broadcast and directly send data to other sockets, and also join/leave rooms through a basic client-side API.

Setup

NPM

$ npm install gsockets

Server

var gsockets = require('gsockets');

// to enable debug logs, add this line
gsockets.config({ log: true});

// express middleware to host the client.js file at /gsockets/client.js
app.use(gsockets.client);

Using existing Socket.IO

io.on('connect', function(socket){
	gsockets.bind(io, socket);
});

Using standalone mode

// choose whatever port number you want to use
gsockets.listen(1234);

Client

<script src="https://cdn.socket.io/socket.io-1.0.6.js"></script>
<script src="/gsockets/client.js"></script>
<script>
	gsockets.connect();

	gsockets.broadcast('user_connected');

	gsockets.on('user_connected', function(){
		console.log('A user has connected');
	});
</script>

Server API

gsockets.client(req, res, next)

Serve the client.js file at the url /gsockets/client.js

  • usage: app.use(gsockets.client);

gsockets.config(obj)

Modify configuration settings of gsockets. The only option currently available is log.

  • usage: gsockets.config({ log: true});

gsockets.bind(io, socket)

Add the gsockets events to your socket.io server

gsockets.listen(port)

Create a separate instance of socket.io especially to run gsockets.

Client API

gsockets.connect([ip], [port])

Create the connection to the socket.io server

  • ip (optional) The ip of the server if different from the current page
  • port (optional) The port of the server

gsockets.socket

The orginal socket.io object.

  • usage: var socketID = gsockets.socket.id;

gsockets.on(event, callback)

Run the callback when an event is emited from the server

  • event The name of the event to listen for
  • callback The function to run when this event is fired

gsockets.onDisconnect(callback)

Run the callback when a socket disconnects

  • callback The function to run when this event is fired

gsockets.announce(event, [data])

Emit event to all sockets

  • event The name of the event that will be sent to another client
  • data (optional) Data object

gsockets.bounce(event, [data])

Emit event to this socket

  • event The name of the event that will be sent to another client
  • data (optional) Data object

gsockets.send(socketID, event, [data])

Emit event to specific socket

  • socketID The socket id to send this data to
  • event The name of the event that will be sent to another client
  • data (optional) Data object

gsockets.broadcast(event, [data])

Emit event to every socket except this one

  • event The name of the event that will be sent to another client
  • data (optional) Data object

gsockets.roomAnnounce(roomName, event, [data])

Emit event to room

  • roomName The name of the room to emit to
  • event The name of the event that will be sent to another client
  • data (optional) Data object

gsockets.roomBroadcast(roomName, event, [data])

Emit event to everyone in room except you

  • roomName The name of the room to emit to
  • event The name of the event that will be sent to another client
  • data (optional) Data object

gsockets.join(roomName, event)

Join room and emit an event to this socket

  • roomName The name of the room to join
  • event The name of the event that will be sent to another client

gsockets.leave(roomName, event)

Leave room and emit an event to this socket

  • roomName The name of the room to leave
  • event The name of the event that will be sent to another client