0.9.0 • Published 4 years ago

ws-server-client v0.9.0

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

WebSocket Server & Client

This is still a work in progress and very much in the early stages of development. Features & code are subject to change as I continue to build my knowledge in this area.

Installation

yarn add ws-server-client

or

npm install ws-server-client

Getting Started

Add the following to your package.json

"scripts": {
  "start": "node ./node_modules/ws-server-client/start.js"
},

Create a .env file in the root directory of your project

APP_PORT=21000
APP_ENV=local
APP_HOST=127.0.0.1
APP_SCHEME=http
APP_KEY=changeme

CORS_HEADERS=
CORS_ORIGIN=
CORS_METHODS=
CORS_CREDENTIALS=

STORAGE_DRIVER=redis
SESSION_DRIVER=memory

REDIS_HOST=127.0.0.1
REDIS_PORT=6379
REDIS_PASSWORD=
REDIS_UNIX_PATH=

Configuration

Here are a full list of all supported configuration options.

Name.env NameTypeDefaultDescription
app.envAPP_ENVNumberlocalThe environment the application is currently running on.
app.portAPP_PORTNumber21000The port which the server listens to connections on.
app.keyAPP_KEYStringchangemeThe key used for sessions and other forms of encryption.
app.hostAPP_HOSTString127.0.0.1The host which the server listens to connections on.
app.schemeAPP_SCHEMEStringhttpThe scheme/protocol which the server listens to connections on.
client.urlCLIENT_URLStringhttp://localhostThe base URL of the client where the connection is made from.
api.urlAPI_URLStringhttp://localhostThe base URL for the API endpoints.
auth.endpoints.user.urlStringnullThe endpoint to retrieve the authenticated user.
auth.endpoints.user.propertyStringdataThe property to pull the user data from the response.
cors.headersCORS_HEADERSString*The cross-site headers allowed.
cors.originCORS_ORIGINString*The cross-site origin allowed.
cors.methodsCORS_METHODSString*The cross-site methods allowed.
cors.credentialsCORS_CREDENTIALSBooleantrueThe cross-site credentials allowed.
storage.driverSTORAGE_DRIVERStringredisThe driver to determine the type of storage system. Supported values: redis
storage.prefixString<app.env>_The prefix applied on all storage values.
session.driverSESSION_DRIVERStringmemoryThe driver to determine the type of session system. Supported values: memory, redis
session.expiryString60The expiry time for each session in minutes.
redis.hostREDIS_HOSTString127.0.0.1The host for establishing the redis connection.
redis.portREDIS_PORTNumber6379The port for establishing the redis connection.
redis.passwordREDIS_PASSWORDStringnullThe password for authentication when establishing the redis connection.
redis.unix_pathREDIS_UNIX_PATHStringnullThe unix socket path for establishing the redis connection.

Events

Initial setup

Create your event classes in an events directory at the root of your project and the server will automatically import these.

  • The listen(socket, payload) method MUST be implemented to handle the event and return data back. This method can return a number, object, array or string. \
  • The authorize(socket, payload) method can be implemented to determine where the socket is authorized. Defaults to true if not specified. This method must return a boolean.
  • The namespace() method can be implemented to specify which namespace to restrict the event to. This method must return a string.
const { Event } = require('ws-server-client');

class ExampleEvent extends Event {
  authorize(socket, payload) {
    return true;
  };

  listen(socket, payload) {
    return { example: 'This is an example data response.' };
  };

  namespace() {
    return 'ExampleNamespace';
  };
};

module.exports = ExampleEvent;

The storage instance can be used to set and get values through your selected storage driver (Only redis is currently supported)

this.storage.set('example-key', 'example-value').then(...).catch(...);
this.storage.get('example-key').then((value) => ...).catch(...);

The config instance can be used to get configuration values. The key specified uses dot-notation.

this.config.get('app.env');

Send to server

All events will be sent to the server using the Socket IO client library.

io.emit('ExampleEvent', { example: 'Example payload.'}, data => {});

Namespaces

Initial setup

Create your namespace classes in a namespaces directory at the root of your project and the server will automatically import these.

  • The authorize(socket, { query }) method can be implemented to determine whether the socket is authorized to connect. Defaults to true if not specified. This method must return a boolean.
  • The connect(socket, { query }) method can be implemented to handle a new socket connection. This method doesn't have to return anything.
  • The disconnect(socket) method can be implemented to handle a socket being disconnected. This method doesn't have to return anything.
const { Namespace } = require('ws-server-client');

class ExampleNamespace extends Namespace {
  authorize(socket, { query }) {
    return true;
  };

  connect(socket, { query }) {
    socket.emit('InitialData', { example: 'Example payload if you want to send the client initial data.' });
  };
 
  disconnect(socket) {};
};

module.exports = ExampleNamespace;

License

MIT

0.9.0

4 years ago

0.8.0

4 years ago

0.7.2

4 years ago

0.7.1

4 years ago

0.6.1

5 years ago

0.6.0

5 years ago

0.5.0

5 years ago

0.4.1

5 years ago

0.4.0

5 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.0

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago