6.5.5 • Published 10 months ago

socket-sorcerer v6.5.5

Weekly downloads
12
License
ISC
Repository
github
Last release
10 months ago
npm i -s socket-sorcerer;

Socket Sorcerer is a light-weight WebSocket framework based on 'ws' package.

In comparison with other libraries it has next advantages:

  • already implemented reconnects, pinging, authentication etc
  • the simplest implementation of full functionality in less rows of Your code
  • easily integration in any application

Build server:

import { WebSocketServer } from 'socket-sorcerer/server';
import { createServer } from 'http';

const server = createServer().listen(8088);

const wss = new WebSocketServer({
  serverOptions: {
    server
  },
  pingInterval: 5000,
  pingTimeout: 15000,
  authenticate: {
    eventName: 'auth',
    async eventHandler(token) {
      // token may be simple string or object contains string "token" property and other parameters
      const user = { _id: '5e1c62a969a07513e8f99a73' };
      return user._id;
    },
    authTimeout: 3000
  },
  events: {
    connect (connectionId, userId, token, userIp) {
      // event is emitting once after successful authentication
      wss.getManager().join({
        channel: 'my awesome channel',
        user: uid
      });
      wss.getManager().join({
        channel: 'for MacOS users only',
        connection: connectionId
      });
    },
    flud (data, connectionId, userId) {
      //process the data
    },
    disconnect (connectionId, userId, token) {
      // event is emitting when socket is closed
    },
    afterConnect (connectionId, userId, token) {
      // event is emitting  right after connect event and at each successful ping-pong
    }
  }
});

// Built server has its own manager property which You can export end use anywhere in the code:

const manager = wss.getManager();
manager.send({
  channel: 'for MacOS users only',
  event: 'update',
  data: {
    version: '1.2.3',
    downloadUrl: '...'
  }
});
Server Options:
  • serverOptions: see ws.ServerOptions
  • pingInterval: interval between to ping probes in milliseconds,
  • pingTimeout: value when connection will be closed after ping probe fails in milliseconds,
  • authenticate.eventName: Your custom authenticate event name, should be implemented on client side too,
  • authenticate.eventHandler: Your custom authenticate event handler which should take provided from client side token and return user id
  • authenticate.authTimeout: value when connection will be closed after auth probe fails in milliseconds
  • events: Server side event handlers, all of them besides 'connect' and 'disconnect' take two args (data and uid, "connect" and "disconnect" events take one arg - uid). You can use '*' to define middleware which will be called previously (except "connect", "disconnect" and service events)
Build client at the front end side:
import { WebSocketClient } from 'socket-sorcerer/client';

const ws = new WebSocketClient({
  serverUrl: 'ws://localhost:8088',
  token: 'clientPersonalConnectToken',
  doReconnectOnClose: true,
  reconnectInterval: 5000,
  authEventName: 'auth',
  events: {
    test: (data) => {
      ws.emit({
        channel: 'test',
        event: 'message',
        data: {
          message: 'Hi there'
        }
      })
    }
  }
});

// You can export ws object and use it anywhere, e.g. just send something to the server 
 ws.emit({
   channel: 'flud',
   event: 'message',
   data: {
     message: 'Hi there'
   }
 });
Client Options:
  • serverUrl: Previously created WebSocket Server URL, f.e. 'ws://localhost:8088',
  • token: Personal token for authentication
  • doReconnectOnClose: Boolean identifier which indicate to do reconnects, recommended value is true,
  • reconnectInterval: Interval between reconnect attempts in milliseconds,
  • authEventName: Authenticate event name the same as the server side,
  • events: Client side event handlers
6.5.0

10 months ago

6.4.5

10 months ago

6.5.2

10 months ago

6.5.1

10 months ago

6.5.4

10 months ago

6.5.3

10 months ago

6.5.5

10 months ago

6.3.0

1 year ago

6.3.1

1 year ago

6.4.3

1 year ago

6.4.2

1 year ago

6.4.4

1 year ago

6.4.1

1 year ago

6.4.0

1 year ago

6.2.1

1 year ago

6.2.0

1 year ago

6.1.12

1 year ago

6.1.11

1 year ago

6.1.10

1 year ago

6.1.8

1 year ago

6.1.9

1 year ago

6.1.6

1 year ago

6.1.7

1 year ago

6.1.5

1 year ago

6.1.2

1 year ago

6.1.1

1 year ago

6.1.4

1 year ago

6.1.3

1 year ago

6.1.0

1 year ago

6.0.9

1 year ago

6.0.10

1 year ago

6.0.7

1 year ago

6.0.6

1 year ago

6.0.8

1 year ago

6.0.1

1 year ago

6.0.0

1 year ago

6.0.3

1 year ago

6.0.2

1 year ago

6.0.5

1 year ago

6.0.4

1 year ago

5.1.1

2 years ago

5.0.0

2 years ago

4.1.4

2 years ago

4.1.0

2 years ago

4.1.2

2 years ago

4.1.1

2 years ago

3.3.1

2 years ago

3.3.0

2 years ago

3.3.5

2 years ago

3.3.4

2 years ago

3.3.3

2 years ago

3.3.2

2 years ago

4.0.0

2 years ago

3.2.2

2 years ago

3.2.1

2 years ago

3.1.2

2 years ago

3.2.0

2 years ago

3.1.1

2 years ago

3.1.0

2 years ago

3.0.0

2 years ago

2.1.5

3 years ago

2.1.4

4 years ago

2.1.3

4 years ago

2.1.2

5 years ago

2.1.1

5 years ago

2.1.0

5 years ago

2.0.24

5 years ago

2.0.23

5 years ago

2.0.22

5 years ago

2.0.21

5 years ago

2.0.20

5 years ago

2.0.19

5 years ago

2.0.17

5 years ago

2.0.18

5 years ago

2.0.16

5 years ago

2.0.15

5 years ago

2.0.14

5 years ago

2.0.13

5 years ago

2.0.12

5 years ago

2.0.11

5 years ago

2.0.10

5 years ago

2.0.9

5 years ago

2.0.8

5 years ago

2.0.7

5 years ago

2.0.6

5 years ago

2.0.5

5 years ago

2.0.4

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.0.0

8 years ago