6.2.1 • Published 27 days ago

socket-sorcerer v6.2.1

Weekly downloads
12
License
ISC
Repository
github
Last release
27 days 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.2.1

27 days ago

6.2.0

27 days ago

6.1.12

27 days ago

6.1.11

28 days ago

6.1.10

1 month ago

6.1.8

1 month ago

6.1.9

1 month ago

6.1.6

2 months ago

6.1.7

2 months ago

6.1.5

3 months ago

6.1.2

3 months ago

6.1.1

3 months ago

6.1.4

3 months ago

6.1.3

3 months ago

6.1.0

3 months ago

6.0.9

3 months ago

6.0.10

3 months ago

6.0.7

4 months ago

6.0.6

4 months ago

6.0.8

4 months ago

6.0.1

4 months ago

6.0.0

4 months ago

6.0.3

4 months ago

6.0.2

4 months ago

6.0.5

4 months ago

6.0.4

4 months ago

5.1.1

6 months ago

5.0.0

6 months ago

4.1.4

8 months ago

4.1.0

8 months ago

4.1.2

8 months ago

4.1.1

8 months ago

3.3.1

10 months ago

3.3.0

10 months ago

3.3.5

10 months ago

3.3.4

10 months ago

3.3.3

10 months ago

3.3.2

10 months ago

4.0.0

8 months ago

3.2.2

12 months ago

3.2.1

1 year ago

3.1.2

1 year ago

3.2.0

1 year ago

3.1.1

1 year ago

3.1.0

1 year ago

3.0.0

1 year ago

2.1.5

2 years ago

2.1.4

2 years ago

2.1.3

3 years ago

2.1.2

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.24

4 years ago

2.0.23

4 years ago

2.0.22

4 years ago

2.0.21

4 years ago

2.0.20

4 years ago

2.0.19

4 years ago

2.0.17

4 years ago

2.0.18

4 years ago

2.0.16

4 years ago

2.0.15

4 years ago

2.0.14

4 years ago

2.0.13

4 years ago

2.0.12

4 years ago

2.0.11

4 years ago

2.0.10

4 years ago

2.0.9

4 years ago

2.0.8

4 years ago

2.0.7

4 years ago

2.0.6

4 years ago

2.0.5

4 years ago

2.0.4

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.0.0

7 years ago