var WebSocket =require('rpc-websockets').Clientvar WebSocketServer =require('rpc-websockets').Server// instantiate Server and start listening for requestsvar server =newWebSocketServer({ port:8080, host:'localhost'})// register an RPC methodserver.register('sum',function(params) {return params[0] + params[1]})// ...and maybe a protected one alsoserver.register('account',function() {return ['confi1','confi2']}).protected()// create an eventserver.event('feedUpdated')// get eventsconsole.log(server.eventList())// emit an event to subscribersserver.emit('feedUpdated')// close the serverserver.close()// instantiate Client and connect to an RPC servervar ws =newWebSocket('ws://localhost:8080')ws.on('open',function() {// call an RPC method with parametersws.call('sum', [5,3]).then(function(result) {require('assert').equal(result,8) })// send a notification to an RPC serverws.notify('openedNewsModule')// subscribe to receive an eventws.subscribe('feedUpdated')ws.on('feedUpdated',function() {updateLogic() })// unsubscribe from an eventws.unsubscribe('feedUpdated')// login your client to be able to use protected methodsws.login({'username':'confi1','password':'foobar'}).then(function() {ws.call('account').then(function(result) {require('assert').equal(result, ['confi1','confi2']) }) }).catch(function(error) {console.log('auth failed') })// close a websocket connectionws.close()})
Documentation
Please consult our API documentation for both the WebSocket server and client JavaScript and TypeScript classes.
All library's open-source features are documented in our API documentation and can be used free of charge. You are free to implement your solutions based on provided methods in any way you are comfortable with, as long as you use our work along our very permissive conditions.