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

ampersand-io

v0.4.2

Published

Provides a socket.io wrapper to be used with ampersand modules

Downloads

13

Readme

Build Status Dependency Status devDependency Status

ampersand-io

Provides a socket.io wrapper to be used with ampersand modules. Developed mainly to be used with ampersand-io-model and ampersand-io-collection

Install

npm install ampersand-io

API Reference

extend AmpersandIO.extend([attributes])

Supports the normal extend behavior (through usage of ampersand-class-extend).

Note that neither the events property nor the listeners property are extendable through this method.

socket IO.socket

Override this property to specify the socket that will be used when a new object reference is created. Useful for when you have a pre-existing socket connection that you would like to use. Defaults to an socket.io-client instance resulting from io.connect().

var io = require('socket.io-client');
var IO = AmpersandIO.extend({
    socket: io()
});

events IO.events

Overridable property containing a key-value reference to the events to be used by the socket conection. Useful for usage with the listeners property and methods, as well as with the emit emit.

events: {
  eventOne: 'my-awesome-event',
  eventTwo: 'my-super-awesome-event'
}

It also supports the usage of arrays of different events tied to a single key.

events: {
  eventArray: ['this-event', 'other-event']
}

listeners IO.listeners

Overridable property containing a set of listeners to be used by the socket connection. The key may be an entirely unreferenced event or one of properties from the events property object. The fn property contains the callback function to be called when the event is fired. The active option is a Boolean that, if set to true, will set this listener to be initialized upon construction.

Note: this property inside the fn callback will be a reference to the whole ampersand-io instance, allowing you to access any property in your object, including the socket object.

events:{
  myEvent: 'thisEvent',
  arrayOfEvents: ['event1', 'event2']
}

listeners: {
	myEvent: {
		fn: function(data, cb){
			console.log('This is an event callback');
			console.log(this.events);
			//Will output:
			//events:{
			//  myEvent: 'thisEvent',
			//  arrayOfEvents: ['event1', 'event2']
			//}
		}
	},
	arrayOfEvents: {
		fn: function(data, cb){
			console.log('This callback will be called when all the events of arrayOfEvents are fired');
		},
		active: true //will be active when the object is created
	},
	'otherEvent': {
		fn: function(data, cb){
			console.log('This event is not listed in the events property');
		}
	}
}

constructor/initialize new AmpersandIO([socket], [options])

When creating an AmpersandIO object, you may choose to pass in either a socket object or a string to be used as a namespace for a new socket.io-client instance. If none of those are provided the AmpersandIO instance will use the socket object defined in the class. Options support a listeners and events objects according to the ones mentioned above (note that these will override the class definitions) and also an initListeners prop which, if passed true, will set the class listeners active.

var IO = new AmpersandIO('chat', {
	events: {
		receive: 'new-message'
	},
	listeners: {
		receive: {
			fn: function(data, cb){
				console.log('New message: ' + data);
			},
			active: true
		}	
	}
});

addListeners IO.addListeners(listeners)

Add a set of listeners to the listeners property of the object. Pass a listeners object as described above. If a listener by that name already exists and is currently active it should be removed first or else it will be ignored.

setListeners IO.setListeners([listeners])

Set the given listeners active. Accepts an array of strings containing the names of the events associated with the listeners. If no argument is provided the method will set all the listeners from the current object. Nothing done to listeners which are already active.

// sets the listeners associated with the receive and send events
IO.setListeners(['receive', 'send']);

// sets all the listeners in the IO object
IO.setListeners();

removeListeners IO.removeListeners([listeners])

Sets the given listeners unactive. Accepts an array of strings containing the names of the events associated with the listeners. If no argument is provided the method will remove all the listeners from the current object. Nothing done to listeners which are already unactive.

Note: the respective properties from the listeners property aren't deleted. The active property is set to false.

// removes the listeners associated with the receive and send events
IO.removeListeners(['receive', 'send']);

// removes all the listeners in the IO object
IO.removeListeners();

emit IO.emit(event, data, [options], [callback])

Method responsible for emitting events. The event name may be one of the events listed in the events property or other of your choice. The data sent to the socket connections will be an object {data: data, options: options} containing the arguments passed to this function. Pass options.room if you want to emit to a particular room and an options.callback that will be also passed to the socket emit method. You may choose to pass the callback function directly as an argument (options.callback will be ignored in this case).

IO.emit('send', 'hi', function(){
	console.log('acked hi');
});

credits

Created by @JGAntunes, with the support of @SINFO and based on a series of Ampersand Modules.

license

MIT