jssocket
ES6 JS classes for socket client and server message passing
Installation
npm install jssocket --save
Usage
This is an example of how to write a chat with this socket module.
One core idea is the "type" key is used for determining what callback runs for what message
Another core feature is that messages are queued if attempted to be sent when disconnected.
Those messages will be sent upon connecting/reconnecting.
// Server // Instantiate server socket listening on port 8000var jssocket = ;var sockServ = 8000;var delimiter = "\n"; // Attach handlers for receiving messagessockServ;
// Client // Instantiate client socket to connect to 'yourdomain.com:8000'var jssocket = ;jssocket; // Upon a disconnection or failed connect attempt, it will retry every// second as long as jssocket.continueReconnecting is truejssocketcontinueReconnecting = true; // Specify a callback to be run when this client receives a message// Overriding the existing function onSocketMessage. Inheritance would also workjssocket { try var message = JSON; catch e return console; console; // find the type and respond appropriately if messagetype == "NEW_CHAT_MESSAGE" console; else if messagetype == "CHAT_ACKNOWLEDGEMENT" console; else console; }; // Start trying to connectjssocket; // An application would send a chat message with:jssocket;