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 = ;var sockjs = ;// Require the wrappervar Connection = new ; var echo = sockjs;var server = http;echo;server; // Attach the sockjs server to the wrapper Connectionstartecho; // Now you can listen to your own custom eventsConnection;
And the client
// Require the client or make sure the client.js is in your html before this code runsvar Connection = new ; // On successful connectionConnection; // Start the connection// If the SockJS client is in your HTML before you startConnectionstart port: 9876 sockjs_path: '/echo'; // Or if you require the SockJS client:var SockJS = ;ConnectionstartSockJS 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 onConnectionstartSockjs_echo authentication: true;Connection; // Connection fires authenticated event when successfulConnection;
And the Client
Connection; Connection;
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