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
npm install sockjs-wrap
Run the full example:
npm run example
Run mocha tests:
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 wrapperConnectionstartecho;// 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 startConnectionstartport: 9876sockjs_path: '/echo';// Or if you require the SockJS client:var SockJS = ;ConnectionstartSockJSport: 9876sockjs_path: '/echo';
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
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
as a second parameter to the start function.
Predefined events (both client and server):