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

es-wrapper

v0.1.0

Published

ESWrapper makes your express.js powered API accessible through real-time sockets with Socket.io.

Downloads

5

Readme

Express Socket.io Wrapper

ESWrapper for short

logo image

ESWrapper makes your express.js powered API accessible through real-time sockets with Socket.io.

[Disclaimer: remember, this lib is in the TEST mode right now (not fully production tested)]

Installation

In console:

npm install es-wrapper

In code:

require('es-wrapper');

Usage

Create your standard express.js application, require es-wrapper, just instead of doing app.listen(PORT); do new ESWrapper(app).listen(PORT);.

Don't forget to pass express application as a first parameter to ESWrapper: new ESWrapper(expressApp).

    var app = require('express')();
    var ESWrapper = require('es-wrapper');
    
    // app.listen(8080);
    new ESWrapper(app).listen(8080);
    
    app.get('/', function (req, res) {
      res.sendfile(__dirname + '/index.html');
    });
    
    app.get('/sayHi', function (req, res) {
      res.end('Hello World!');
    });

Now you can access /sayHi route with both http(xhr) and socket.io client. index.html:

    <script src="/socket.io/socket.io.js"></script>
    <script>
      var socket = io.connect('http://localhost:8080');
    
      socket.emit('request', {
        path: '/sayHi',
        method: 'get',
        headers: {},
        body: {}
      });
    
      socket.on('response', function (res) {
        alert(res.body); // Hello World!
      });
    
    </script>
    

And still have access with http:

    <script src="http://code.jquery.com/jquery-1.12.1.min.js"></script>
    <script>
      $.get('http://localhost:8080/sayHi', function(data) {
        alert(data); // Hello World!
      });
    </script>
    

Need access to raw socket.io? No problem:

    var express = require('express');
    var ESWrapper = require('es-wrapper');
    var wrapper = new ESWrapper(express());
    var app = wrapper.app;
    var io = wrapper.io;
    
    wrapper.listen(); // default port will be 80
    
    app.get('/', function(req, res) {
      res.sendfile(__dirname + '/index.html');
    });
    
    io.on('connection', function(socket) {
      socket.emit('hello', { hello: 'world' });
    });
   

Separate socket logic & http. Req will have isSocket: true value and socketIO object witch is socket.io connected socket. Also req.connection is same as socketIO.

    app.delete('/socketIOtest', function(req, res) {
      if (req.isSocket) { // req.isSocket is always true for socket requests
        req.socketIO.emit('extra', 200); // req.socketIO is socket received on connection
        req.connection.emit('extra', 200); // same here
      }
      res.sendStatus(200);
    });

wrapper.client.io.js

Another alternative is to use wrapper.client.io.js. It is small wrapper to provide REST methods and callbacks, to better emulate REST http API.

wrapper.client.io.js can be used both in node.js and browser

node:

    var ESWrapper = require('es-wrapper');
    var wrapperClient = ESWrapper.IOFactory;
    var SocketClient = require('socket.io-client');
    
    var io = new IOFactory(SocketClient);

io (browser):

Don't forget to grab wrapper.client.io.js from repo root express-socket.io-wrapper/client/io.js and include it in your page however you like.

    var wrapperIO = new IOFactory();
    
    wrapperIO.get('/someUrl', function(res) {});
    wrapperIO.post('/someUrl', {
      data: 'some body data'
    }, {
      'Content-Type': 'application/json'
    }, function(res) {});
    wrapperIO.put('/someUrl', function(res) {});
    wrapperIO.delete('/someUrl', function(res) {});
    

new IOFactory(socketClient)

| Param | Type | | --- | --- | | socketClient | Object |

socketClient is optional, if using in browser and socektClient is not provided, IOFactory will look for global io object

ioFactory.init(socketClient, [opt_url])

Kind: instance method of IOFactory

| Param | Type | | --- | --- | | socketClient | Object | | [opt_url] | String |

ioFactory.request(options, cb)

Kind: instance method of IOFactory

| Param | Type | | --- | --- | | options | Object | | cb | function |

ioFactory.get(path, [opt_headers], cb)

Kind: instance method of IOFactory

| Param | Type | | --- | --- | | path | String | | [opt_headers] | Object | | cb | function |

ioFactory.post(path, [opt_body], [opt_headers], cb, [opt_method])

Kind: instance method of IOFactory

| Param | Type | | --- | --- | | path | String | | [opt_body] | * | | [opt_headers] | Object | | cb | function | | [opt_method] | String |

ioFactory.put(path, [opt_body], [opt_headers], cb)

Kind: instance method of IOFactory

| Param | Type | | --- | --- | | path | String | | [opt_body] | * | | [opt_headers] | Object | | cb | function |

ioFactory.delete(path, [opt_body], [opt_headers], cb)

Kind: instance method of IOFactory

| Param | Type | | --- | --- | | path | String | | [opt_body] | * | | [opt_headers] | Object | | cb | function |

Idea

ESWrapper will:

  • "wrap" express.js with socket.io. Socket.io client can make http-like requests and wrapper will map them to express.js corresponding route functions.
  • get request events, emulate http request for express.js routes and send http response to a client as response event.
  • let you move to sockets from http only express.js(or express.js-like) servers.
  • make it super easy to setup and start using socket.io without creating tons of events and event-handlers.

right now ALPHA stage

TODO

  • common sessions

Credits

This was definitely expired by sails.js a great MVC framework, if you are looking for "all-in-one" solution, you should check them out.

Have a nice Hacking