Coder Social home page Coder Social logo

socketio-server's Introduction

socketio-server

A tool to easily create and use SocketIO rooms inside nodejs routes.

Please Note

This package was built upon 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 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);
    });
});

Inside the NodeJS server, the registered socket can be retrieved at any time using the identifier

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

socketio-server's People

Contributors

raphaelbs avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.