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

sockjs-wrap

v1.6.0

Published

Wrapper for SockJS connections on Node - Add events and client callbacks.

Downloads

34

Readme

SockJs-Wrap

A simple SockJS wrapper for Node.

* Client callbacks and custom data events. 
* Authentication of clients, and keeping track of them. 
* Send, broadcast and broadcastTo functionality. 
* The server and client are both made the Node way.
* Using browser and server compatible Event module EventEmitter3

How to use

To install sockjs-wrap run:

npm install sockjs-wrap

Run the full example: npm run example Run mocha tests: npm test

Simple example

A simple example taken from the SockJS-node page with the new wrapper instead of handeling events directly.

var http = require('http');
var sockjs = require('sockjs');
// Require the wrapper
var Connection = new (require('sockjs-wrap'))();

var echo = sockjs.createServer({ sockjs_url: 'http://cdn.jsdelivr.net/sockjs/0.3.4/sockjs.min.js' });
var server = http.createServer();
echo.installHandlers(server, {prefix:'/echo'});
server.listen(9876, '0.0.0.0');

// Attach the sockjs server to the wrapper 
Connection.start(echo);

// Now you can listen to your own custom events
Connection.on('some_event_or_something_else', function(data, callback){
    console.log("Some data received : ", data);
	// And execute callbacks to the client
    callback({hello: "Hi"});
    // Send to all clients
    Connection.broadcast('some_event', {some: 'data'});
});

And the client

// Require the client or make sure the client.js is in your html before this code runs
var  Connection  = new (require('sockjs-wrap/client'))();

// On successful connection
Connection.on('connect', function(){
    // Send any event you want, with an oject of data and an optional callback
    Connection.send('some_event_or_something_else', {data: "Hello there"}, function(data){
        console.log("The server called our callback: ", data);
    });
});

// Start the connection
// If the SockJS client is in your HTML before you start
Connection.start({
    port: 9876,
    sockjs_path: '/echo'
});

// Or if you require the SockJS client:
var SockJS = require('sockjs-client');
Connection.start(SockJS, {
    port: 9876,
    sockjs_path: '/echo'
});

Authentication example

This is a simplified version of how the authentication process works.

... Start the http servers and everything else ...

// Default authentication is off, so turn it on
Connection.start(Sockjs_echo, {authentication: true});
Connection.on('authenticate', function(data, callback){
    // Fetch your user from DB or memory on data.token
    var user = SomeClass.findUserByToken(data.token); // Dummy function
    if(user){
        // The wrapper added an authentication function to the data object if the client wants to authenticate
        // the user object must contain an id property
        if(data.authenticate(user)) {
            callback({result: true});
        }
    }
});

// Connection fires authenticated event when successful
Connection.on('authenticated', function(user){
    // Do anything you want
});

And the Client

Connection.on('connect', function () {
    var my_client_token = "g75184g15438g517g";
    Connection.authenticate(my_client_token);
});

Connection.on('authenticated', function (data) {
    // data will be the data given to the callback function on the server 
    // The user might have failed authentication here so be sure to check data for the result    
});

Any messages send to the server by the client while not authenticated successfully will get an error back. If you want to use authentication you can call Connection.require_authentication = true; before the connection is started, or give {authentication: true} as a second parameter to the start function.

Predefined events (both client and server):

  • connect
  • close
  • authenticate
  • authenticated