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

socketio-server

v1.3.0

Published

A tool to easily create and use SocketIO-Rooms inside nodejs-express-routes.

Downloads

8

Readme

socketio-server

A tool to easily create and use SocketIO-Rooms inside nodejs-express-routes.

Please Note

This package was built upon the great socket.io. Know the api especially the socket section to get the best out of it.


Installation

use your http server as parameter

var http = require('http');
require('socketio-server')(http, {debug: true});

or your server instance of express

var http = require('http');
var server = http.createServer(app);
require('socketio-server')(server, {debug: true});

Usage

In the client of your application, the first step is to emit an event named register with an identifier that you keep track. In the example bellow I've used my username as the identifier:

// this can be found laying in some Angular directive frontend application

// an socket for the room 'chat'
let socket = io.connect('http://localhost:3000/chat');
socket.on('connect', function(socket){
    // here's the 'register' call
    socket.emit('register', 'raphaelbs');

    // ... any other event assign
    socket.on('foo', function(bar){
        console.log(bar);
    });
});

And inside the Node server, the socket of the registered identifier can be retrieved at any time using the

var express = require('express');
var router = express.Router();
var socket = require('socketio-server');
var chatRoom = socket('chat');

router.get('/', function(req, res) {
    // using the common identifier we can retrieve the correcty socket inside the router
	chatRoom('raphaelbs', function(err, socket){
		if(err) return console.error(err);
		// this socket instance is an socket.io socket
		var bar = 10;
		// only the event 'foo' of 'raphaelbs' will receive this 'bar'
		socket.emit('foo', bar);
	});
});

module.exports = router;

Reference

require('socketio-server')(params[, options])

return function(id, callback)

The main entry point of this module.


Constructor argument: params

type string or object

If you need to initialize the module, you should supply the server as argument:

var http = require('http');
var server = http.createServer(app);
require('socketio-server')(server, {debug: true});

If you are in the use stage, the params argument could serve as Room identifier:

var socket = require('socketio-server');
var chatRoom = socket('chat'); //creates (if not created) and exposes a room named 'chat'
var mainRoom = socket(); //creates (if not created) and exposes the default '/' room

Constructor argument: options

type object

key | description | type | default --- | --- | --- | --- debug | Enable verbosity for debuggin (very handy) | boolean | false


require('socketio-server')('room name')(id, callback)

This function is the goal of the module. Within this function, you can specifically get the socket of the desired ID defined in your client-side.

id

type string

The identifier of the socket that you want to retrieve.

callback

type function(err, socket)

This callback function exposes a socket object relative to the identifier above.


Dependencies

socket.io


Contact-me


License

MIT