Provides easy and secure websocket communication mechanisms, including server and client implementation, function call routing, connection management, and tagging.
Go to file
2021-01-23 06:03:54 +00:00
.vscode fix(core): update 2020-09-24 18:04:11 +00:00
test fix(core): update 2021-01-23 04:12:55 +00:00
ts fix(core): update 2021-01-23 05:50:02 +00:00
.gitignore fix(core): update 2020-09-24 18:04:11 +00:00
.gitlab-ci.yml fix(core): update 2020-09-24 18:04:11 +00:00
.snyk fix(core): update 2019-04-24 17:17:36 +02:00
npmextra.json fix(core): update 2020-09-24 18:04:11 +00:00
package-lock.json fix(core): update 2021-01-23 06:03:54 +00:00
package.json fix(core): update 2021-01-23 06:03:54 +00:00
readme.md fix(core): update 2020-12-16 02:20:28 +00:00
tslint.json fix(structure): update to latest standards 2019-01-30 03:14:02 +01:00

@pushrocks/smartsocket

easy and secure websocket communication

Status for master

Status Category Status Badge
GitLab Pipelines pipeline status
GitLab Pipline Test Coverage coverage report
npm npm downloads per month
Snyk Known Vulnerabilities
TypeScript Support TypeScript
node Support node
Code Style Code Style
PackagePhobia (total standalone install weight) PackagePhobia
PackagePhobia (package size on registry) PackagePhobia
BundlePhobia (total size when bundled) BundlePhobia
Platform support Supports Windows 10 Supports Mac OS X

Usage

Use TypeScript for best in class instellisense.

Under the hood we use socket.io and shortid for managed data exchange.

Serverside

import * as smartsocket from 'smartsocket';
import * as q from q; // q is a promise library

// The "Smartsocket" listens on a port and can receive new "SocketConnection" requests.
let mySmartsocket = new smartsocket.Smartsocket({
  port: 3000, // the port smartsocket will listen on
});

// optional:
// run this with anothoer existing server like express
declare var someExpressServer; // read the express docs about how express actually works
mySmartsocket.setServer(someExpressServer);

// A "SocketRole" can be referenced by "SocketFunction"s.
// All "SocketRequest"s carry authentication data for a specific "SocketRole".
// "SocketFunction"s know which "SocketRole"s are allowed to execute them
let mySocketRole = new smartsocket.SocketRole({
  name: 'someRoleName',
  passwordHash: 'someHashedString',
});

// A "SocketFunction" executes a referenced function and passes in any data of the corresponding "SocketRequest".
// The referenced function must return a promise and resolve with data of type any.
// Any "SocketRequest" carries a unique identifier. If the referenced function's promise resolved any passed on argument will be returned to the requesting party
let testSocketFunction1 = new smartsocket.SocketFunction({
  funcName: 'testSocketFunction1',
  funcDef: (data) => {
    console.log('testSocketFunction1 executed successfully!');
  },
  allowedRoles: [mySocketRole], // all roles that have access to a specific function
});

// A "Smartsocket" exposes a .clientCall() that gets
// 1. the name of the "SocketFunction" on the client side
// 2. the data to pass in
// 3. And a target "SocketConnection" (there can be multiple connections at once)
// any unique id association is done internally
mySmartsocket.clientCall('restart', data, someTargetConnection).then((responseData) => {});

Client side

import * as smartsocket from 'smartsocket';

// A "SmartsocketClient" is different from a "Smartsocket" in that it doesn't expose any public address.
// Thus any new "SocketConnection"s must be innitiated from a "SmartsocketClient".
let testSmartsocketClient = new smartsocket.SmartsocketClient({
  port: testConfig.port,
  url: 'http://localhost',
  password: 'testPassword',
  alias: 'testClient1',
  role: 'testRole1',
});

// You can .connect() and .disconnect() from a "Smartsocket"
testSmartsocketClient.connect().then(() => {
  done();
});

// The client can also specify "SocketFunction"s. It can also specify "SocketRole"s in case a client connects to multiple servers at once
let testSocketFunction2 = new smartsocket.SocketFunction({
  funcName: 'testSocketFunction2',
  funcDef: (data) => {}, // the function to execute, has to return promise
  allowedRoles: [],
});

// A "SmartsocketClient" can call functions on the serverside using .serverCall() analog to the "Smartsocket"'s .clientCall method.
mySmartsocketClient.serverCall('function', functionCallData).then((functionResponseData) => {
  // the functionResponseData comes from the server... awesome, right?
});

NOTE:
you can easily chain dependent requests on either the server or client side with promises.
data is always a js object that you can design for your specific needs.
It supports buffers for large binary data network exchange.

Contribution

We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can contribute one time or contribute monthly. :)

For further information read the linked docs at the top of this readme.

MIT licensed | © Lossless GmbH | By using this npm module you agree to our privacy policy

repo-footer