jrf-ws-3
Description
jrf-ws-3 - is a JavaScript library, to create a real time API , based on WebSockets. Is a wrapper over a light and fast. ws.
It consists of a server part, which runs in the node.js. And the client part, which can be run in the node.js and in a web browser.
Messages support routing on server and client. You can send a message in one of 3 options:
- Send the message asynchronously, without waiting for a reply
- Send the message asynchronously, waiting for a reply to the callback
- Send a message asynchronously, waiting for a response in synchronous style
It is possible to send group messages.
The horizontal scaling of the server part is supported.
You can assign addresses of several servers to a client. When the connection is broken, the client will try to connect to servers using the Round-robin algorithm.
To optimize the work, the library's internal mechanisms, use caching and asynchronous parallel iteration processing.
Get started
Server
const jrfWS = ; // Create an instance of the serverconst server = id: 'server' port: 4000; // Add a handler to the server startup eventserver; // Routing // Add processing of all incoming messagesserver; // Add processing of messages with 'add' action on the 'math' route// @param {string} route - Route// @param {string} act - Action on the routeserver; serverstart;
Client
const jrfWS = ; // Create an instance of the clientconst client = id: 'client'url: 'ws://localhost:4000'; // Let's add processing of the 'echo' routeclient; // The client sends messages to the serverconst sendMessages = async { // Send the message asynchronously, without waiting for a replyawait client;// server console.log -> `Processing all incoming messages on the server. route: undefined, act: undefined, data: {description: 'test message'}`// client console.log -> `echo data: {description: 'test message'}` // Send the message asynchronously, waiting for a response in synchronous styleconst res = await client;// server console.log -> `Processing all incoming messages on the server. route: 'math', act: 'add', data: {a: 1, b: 2}`// client console.log -> `echo data: {a: 1, b: 2}`console // -> 3 // Send the message asynchronously, waiting for a response in callbackconst cbAfterRes = { console; // -> 8}; await client;// server console.log -> `Processing all incoming messages on the server. route: 'math', act: 'add', data: {a: 5, b: 3}`// client console.log -> `echo data: {a: 5, b: 3}` }; // Add a handler to the event when a connection is opened between the server and the clientclient; clientstart;