Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/raphaelbs/socketio-server
A tool to easily create and use SocketIO-Rooms inside nodejs-express-routes.
https://github.com/raphaelbs/socketio-server
nodejs-modules npm-package session-management socketio socketio-server
Last synced: 2 days ago
JSON representation
A tool to easily create and use SocketIO-Rooms inside nodejs-express-routes.
- Host: GitHub
- URL: https://github.com/raphaelbs/socketio-server
- Owner: raphaelbs
- Created: 2016-09-17T20:00:02.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-10-27T21:52:36.000Z (about 6 years ago)
- Last Synced: 2024-04-24T10:14:32.934Z (7 months ago)
- Topics: nodejs-modules, npm-package, session-management, socketio, socketio-server
- Language: JavaScript
- Size: 10.7 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# socketio-server
A tool to easily create and use SocketIO rooms inside nodejs routes.#### Please Note
This package was built upon [socket.io](https://www.npmjs.com/package/socket.io).
Know the [api](https://www.npmjs.com/package/socket.io#api) especially the [socket](https://www.npmjs.com/package/socket.io#socket) section to get the best out of it.## Installation
use your http server as parameter
```javascript
var http = require('http');
require('socketio-server')(http, {debug: true});
```or your server instance of express
```javascript
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:
```javascript
// 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
```javascript
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](#constructor-argument-params)[, [options](#constructor-argument-options)])
>_return [function(id, callback)](#requiresocketio-serverroom-nameid-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:
```javascript
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**:
```javascript
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 stringThe identifier of the socket that you want to retrieve.
#### callback
> type function(err, socket)This callback function exposes a [socket](https://www.npmjs.com/package/socket.io#socket) object relative to the identifier above.
## Dependencies
socket.io
## Contact-me
* [Email](mailto:[email protected])
* [Facebook](https://facebook.com/raphaelbs)
* [GitHub](https://github.com/raphaelbs)
* [NPM](https://npmjs.com/~raphaelbs)## License
MIT