2016-08-06 23:27:53 +02:00
# smartsocket
2016-08-07 19:07:11 +02:00
easy and secure websocket communication, Typescript ready
2016-08-06 23:27:53 +02:00
2016-09-03 16:03:57 +02:00
## Availabililty
[](https://www.npmjs.com/package/smartsocket)
[](https://gitlab.com/pushrocks/smartsocket)
[](https://github.com/pushrocks/smartsocket)
[](https://pushrocks.gitlab.io/smartsocket/docs)
## Status for master
2016-08-06 23:27:53 +02:00
[](https://gitlab.com/pushrocks/smartsocket/commits/master)
2016-09-03 16:03:57 +02:00
[](https://gitlab.com/pushrocks/smartsocket/commits/master)
[](https://david-dm.org/pushrocks/smartsocket)
[](https://www.bithound.io/github/pushrocks/smartsocket/master/dependencies/npm)
[](https://www.bithound.io/github/pushrocks/smartsocket)
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
2016-08-06 23:27:53 +02:00
## Usage
We recommend the use of typescript.
2016-08-07 19:07:11 +02:00
Under the hood we use socket.io and shortid for managed data exchange.
2016-08-06 23:27:53 +02:00
2016-08-07 15:12:21 +02:00
### Serverside
2016-08-07 14:58:20 +02:00
```typescript
2016-08-07 15:14:53 +02:00
import * as smartsocket from "smartsocket";
2016-09-26 00:23:17 +02:00
import * as q from q // q is a promise library
2016-08-07 15:14:53 +02:00
2016-09-26 00:23:17 +02:00
// The smartsocket listens on a port and can receive new socketconnection requests.
2016-08-07 14:58:20 +02:00
let mySmartsocket = new smartsocket.Smartsocket({
port: 3000 // the port smartsocket will listen on
});
2016-09-26 00:23:17 +02:00
// A socket role can be referenced by SocketFunctions.
// All SocketRequests carry authentication data for a specific role.
// SocketFunctions now which roles are allowed to execute them
2016-08-07 14:58:20 +02:00
let mySocketRole = new smartsocket.SocketRole({
name: "someRoleName",
2016-08-07 15:12:21 +02:00
passwordHash: "someHashedString"
2016-08-07 14:58:20 +02:00
});
2016-09-26 00:23:17 +02:00
// A SocketFunction executes a referenced function and passes in any data of the corresponding request.
// The referenced function must return a promise and resolve with any data
// Any request will be carries a unique identifier. If the referenced function's promise resolved any passed on argument will be returned to the requesting party
2016-08-15 03:47:32 +02:00
let testSocketFunction1 = new smartsocket.SocketFunction({
funcName:"testSocketFunction1",
funcDef:(data) => {
2016-09-26 00:23:17 +02:00
console.log('testSocketFunction1 executed successfully!')
},
2016-08-15 03:47:32 +02:00
allowedRoles:[mySocketRole] // all roles that have access to a specific function
2016-08-07 14:58:20 +02:00
});
2016-09-26 00:23:17 +02:00
// A smartsocket exposes a .clientCall() that gets
// 1. the name of the SocketFunctin on the client side
// 2. the data to pass in
// 3. And a target connection (there can be multiple connections at once)
// any unique id association is done internally
mySmartsocket.clientCall("restart",data,someTargetConnection)
2016-08-07 15:12:21 +02:00
.then((responseData) => {
2016-08-07 14:58:20 +02:00
2016-08-07 15:12:21 +02:00
});
```
#### Client side
```typescript
2016-08-07 15:14:53 +02:00
import * as smartsocket from "smartsocket";
2016-09-26 00:23:17 +02:00
// A SmartsocketClient is different from a Smartsocket in that it doesn't expose any public address
// Thus any new connections must be innitiated from the client
2016-08-15 03:47:32 +02:00
let testSmartsocketClient = new smartsocket.SmartsocketClient({
port: testConfig.port,
url: "http://localhost",
password: "testPassword",
alias: "testClient1",
role: "testRole1"
2016-08-07 14:58:20 +02:00
});
2016-09-26 00:23:17 +02:00
// You can .connect() and .disconnect() from a Smartsocket
2016-08-15 03:47:32 +02:00
testSmartsocketClient.connect()
.then(() => {
done();
});
2016-08-07 14:58:20 +02:00
2016-09-26 00:23:17 +02:00
// The client can also specify SocketFunctions. It can also specify Roles in case a client connects to multiple servers at once
2016-08-15 03:47:32 +02:00
let testSocketFunction2 = new smartsocket.SocketFunction({
funcName: "testSocketFunction2",
funcDef: (data) => {}, // the function to execute, has to return promise
allowedRoles:[]
2016-08-07 14:58:20 +02:00
});
2016-09-26 00:23:17 +02:00
// A SmartsocketClient can call functions on the serverside using .serverCall() analog to the Smartsocket's .clientCall method.
2016-08-15 03:47:32 +02:00
mySmartsocketClient.serverCall("function",functionCallData)
.then((functionResponseData) => { // the functionResponseData comes from the server... awesome, right?
2016-08-07 15:12:21 +02:00
});;
2016-08-07 19:07:11 +02:00
```
2016-08-07 19:15:05 +02:00
> **NOTE:**
2016-08-15 03:47:32 +02:00
you can easily chain dependent requests on either the server or client side with promises.
2016-08-07 19:15:05 +02:00
`data` is always a js object that you can design for your specific needs.
It supports buffers for large binary data network exchange.