eventedsocket

0.0.5 • Public • Published

Eventedsocket

Eventedsocket adds event like behavior to your socket.io connection, allowing for events to be sent from client(s) to server or server to client(s). Your custom events along with the desired data are communicated as json over whatever protocol socket.io might choose.

Installation

$ npm install eventedsocket

API

Emit a custom event, on the form of namespace:eventName, from the client side (as shown here using jade)..

script(src:'/socket.io/socket.io.js')
script(src:'/javascripts/eventedsocket.js')
script
  | var socket = new io.Socket();
  | socket.connect();
  | socket.on('connect', function() { 
  |   var client = evented.client(socket);
  |   client.emit('client:says', {
  |     'whatever': 'hello!'
  |   });

..receive it on the server side (or, of course, the other way around, if thats what floats your boat)

var io = require('socket.io');
var evented = require('eventedsocket');
...
var socket = io.listen(app);
socket.on('connection', function (client) {
  var client = evented.client(client); 

  client.on('client:says', function (json) { 
    console.log(json.whatever); // hello!
  });                                            
});

The same eventedsocket.js file is used on both the server and the client for adding the event-like wrapper around the already awesome socket.io.

Example

A working example expressjs application showing off some more features can be found in the example/ directory. Start it using

$ npm install express 
$ node app.js
$ open http://localhost:3000

Enjoy!

TODO

  • add on-method for server socket
  • add client session id to client.broadcast data

License

(The MIT License)

Copyright (c) <2011> Torgeir Thoresen torgeir.thoresen@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Readme

Keywords

none

Package Sidebar

Install

npm i eventedsocket

Weekly Downloads

7

Version

0.0.5

License

none

Last publish

Collaborators

  • torgeir