1.18.138 • Published 3 months ago

@arcblock/ws v1.18.138

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
3 months ago

Blocklet Server WebSocket

Blocklet Server PubSub base on Websocket and Phoenix Protocol

Usage

  1. WsServer
const { WsServer } = require('@arcblock/ws');

/**
 * @params {Object} opts
 *  @params {http.Server} opts.httpServer
 *  @params {String} opts.pathname default to '/websocket'
 *  @params {Function} opts.authenticate
 */
const wsServer = new WsServer({
  httpServer: http.createServer(),
  authenticate: (req, cb) => {
    const { searchParams } = new URL(req.url, `http://${req.headers.host || 'unknown'}`);
    const token = searchParams.get('token');
    if (!token) {
      cb(new Error('token not found'), null);
      return;
    }

    // custom logic for validate token
    const authInfo = validateToken(token);

    // if validate success
    cb(null, authInfo);

    // if validate error
    cb(new Error('validate fail'), null);
  },
});

// attach to httpServer(httpServer has passed by constructor)
wsSerer.attach();

// push message
wsServer.broadcast('blocklet.installed', data);
wsServer.broadcast('notification.create', data);
wsServer.broadcast('topic', 'event', data);
  1. WsClient

WsClient is inherited from Phoenix(source),

import WsClient from '@arcblock/ws/lib/client';

// create instance
const socket = new WsClient(`//${window.location.hostname}`, {
  // params will be passed to server through url
  params: () => ({
    // token is used for authentication
    token: window.localStorage.getItem('abt_node_login_token'),
  }),

  // Defaults to none
  logger: (type, msg, data) => console.log(type, msg, data),
});

// connect
socket.connect();

// add subscriber
socket.on('blocklet.installed', callback1);
socket.on('notification.create', callback2);

// remove subscriber
socket.off('blocklet.installed', callback1);
socket.off('notification.create', callback2);

// More flexible subscription
const subscription = socket.subscribe('topic', {});
subscription.on('event', ({ response }) => {
  // Do something with the event data
});

// disconnect
socket.disconnect(() => {
  // after disconnected...
});
  1. Hooks

It's very simple to create hooks in react apps.

1.18.138

3 months ago

1.18.137

3 months ago

1.18.136

3 months ago

1.18.135

5 months ago

1.18.134

5 months ago

1.18.133

5 months ago

1.18.132

5 months ago

1.18.131

5 months ago

1.18.130

5 months ago

1.18.129

5 months ago

1.18.128

6 months ago

1.18.127

6 months ago

1.18.126

7 months ago

1.18.125

7 months ago

1.18.124

7 months ago

1.18.123

8 months ago

1.18.122

8 months ago

1.18.121

8 months ago

1.18.120

8 months ago

1.18.119

8 months ago

1.18.118

8 months ago

1.18.117

9 months ago

1.18.116

9 months ago

1.18.115

10 months ago

1.18.114

10 months ago

1.18.113

11 months ago

1.18.112

11 months ago

1.18.111

11 months ago

1.18.110

12 months ago

1.18.109

12 months ago

1.18.108

1 year ago

1.18.107

1 year ago

1.18.106

1 year ago

1.18.105

1 year ago

1.18.104

1 year ago

1.18.96

1 year ago

1.18.99

1 year ago

1.18.98

1 year ago

1.18.97

1 year ago

1.18.103

1 year ago

1.18.102

1 year ago

1.18.101

1 year ago

1.18.100

1 year ago

1.18.95

1 year ago

1.18.94

1 year ago

1.18.93

1 year ago

1.18.85

2 years ago

1.18.89

1 year ago

1.18.88

1 year ago

1.18.87

2 years ago

1.18.86

2 years ago

1.18.92

1 year ago

1.18.91

1 year ago

1.18.90

1 year ago

1.18.81

2 years ago

1.18.84

2 years ago

1.18.83

2 years ago

1.18.82

2 years ago

1.18.80

2 years ago

1.18.70

2 years ago

1.18.74

2 years ago

1.18.73

2 years ago

1.18.72

2 years ago

1.18.71

2 years ago

1.18.78

2 years ago

1.18.77

2 years ago

1.18.76

2 years ago

1.18.75

2 years ago

1.18.79

2 years ago

1.18.69

2 years ago

1.18.67

2 years ago

1.18.66

2 years ago

1.18.68

2 years ago

1.18.41

2 years ago

1.18.40

2 years ago

1.18.45

2 years ago

1.18.44

2 years ago

1.18.43

2 years ago

1.18.42

2 years ago

1.18.49

2 years ago

1.18.48

2 years ago

1.18.47

2 years ago

1.18.46

2 years ago

1.18.38

2 years ago

1.18.39

2 years ago

1.18.63

2 years ago

1.18.62

2 years ago

1.18.61

2 years ago

1.18.60

2 years ago

1.18.65

2 years ago

1.18.64

2 years ago

1.18.52

2 years ago

1.18.51

2 years ago

1.18.50

2 years ago

1.18.56

2 years ago

1.18.55

2 years ago

1.18.54

2 years ago

1.18.53

2 years ago

1.18.59

2 years ago

1.18.58

2 years ago

1.18.57

2 years ago

1.18.34

2 years ago

1.18.33

2 years ago

1.18.37

2 years ago

1.18.36

2 years ago

1.18.35

2 years ago

1.18.1

2 years ago

1.18.0

2 years ago

1.18.5

2 years ago

1.18.4

2 years ago

1.18.3

2 years ago

1.18.2

2 years ago

1.18.9

2 years ago

1.18.8

2 years ago

1.18.7

2 years ago

1.18.6

2 years ago

1.18.30

2 years ago

1.18.32

2 years ago

1.18.31

2 years ago

1.17.23

2 years ago

1.18.23

2 years ago

1.18.22

2 years ago

1.18.21

2 years ago

1.18.20

2 years ago

1.18.27

2 years ago

1.18.26

2 years ago

1.18.25

2 years ago

1.18.24

2 years ago

1.18.28

2 years ago

1.18.12

2 years ago

1.18.11

2 years ago

1.18.10

2 years ago

1.18.16

2 years ago

1.18.15

2 years ago

1.18.14

2 years ago

1.18.13

2 years ago

1.18.19

2 years ago

1.18.18

2 years ago

1.18.17

2 years ago

1.17.22

2 years ago

1.17.21

2 years ago

1.17.20

2 years ago

1.17.15

2 years ago

1.17.14

2 years ago

1.17.13

2 years ago

1.17.12

2 years ago

1.17.19

2 years ago

1.17.18

2 years ago

1.17.17

2 years ago

1.17.16

2 years ago

1.16.10

3 years ago

1.16.14

3 years ago

1.16.13

3 years ago

1.16.12

3 years ago

1.16.17

3 years ago

1.16.16

3 years ago

1.16.15

3 years ago

1.17.11

2 years ago

1.17.10

2 years ago

1.6.10

3 years ago

1.17.2

3 years ago

1.17.1

3 years ago

1.17.0

3 years ago

1.17.6

2 years ago

1.17.5

3 years ago

1.17.4

3 years ago

1.17.3

3 years ago

1.17.9

2 years ago

1.17.8

2 years ago

1.17.7

2 years ago

1.16.7

3 years ago

1.16.6

3 years ago

1.16.5

3 years ago

1.16.9

3 years ago

1.16.8

3 years ago

1.15.0

3 years ago

1.15.4

3 years ago

1.15.3

3 years ago

1.15.2

3 years ago

1.15.1

3 years ago

1.14.20

3 years ago

1.15.7

3 years ago

1.14.21

3 years ago

1.15.6

3 years ago

1.14.22

3 years ago

1.15.5

3 years ago

1.14.23

3 years ago

1.14.24

3 years ago

1.14.25

3 years ago

1.16.3

3 years ago

1.16.2

3 years ago

1.16.1

3 years ago

1.16.0

3 years ago

1.16.4

3 years ago

1.14.19

3 years ago

1.14.1

3 years ago

1.14.0

3 years ago

1.14.5

3 years ago

1.14.4

3 years ago

1.14.3

3 years ago

1.14.2

3 years ago

1.14.9

3 years ago

1.13.75

3 years ago

1.14.8

3 years ago

1.13.74

3 years ago

1.14.7

3 years ago

1.13.77

3 years ago

1.14.6

3 years ago

1.13.76

3 years ago

1.13.71

3 years ago

1.13.70

3 years ago

1.13.73

3 years ago

1.13.72

3 years ago

1.13.79

3 years ago

1.13.78

3 years ago

1.13.66

3 years ago

1.13.65

3 years ago

1.13.68

3 years ago

1.13.67

3 years ago

1.13.69

3 years ago

1.13.80

3 years ago

1.13.86

3 years ago

1.14.10

3 years ago

1.13.85

3 years ago

1.14.11

3 years ago

1.14.12

3 years ago

1.13.82

3 years ago

1.13.81

3 years ago

1.13.84

3 years ago

1.13.83

3 years ago

1.14.17

3 years ago

1.14.18

3 years ago

1.14.13

3 years ago

1.14.14

3 years ago

1.14.15

3 years ago

1.14.16

3 years ago

1.13.57

3 years ago

1.13.56

3 years ago

1.13.59

3 years ago

1.13.58

3 years ago

1.13.64

3 years ago

1.13.63

3 years ago

1.13.60

3 years ago

1.13.62

3 years ago

1.13.61

3 years ago

1.13.53

3 years ago

1.13.52

3 years ago

1.13.55

3 years ago

1.13.54

3 years ago

1.13.51

3 years ago

1.13.50

3 years ago

1.13.49

3 years ago

1.13.46

3 years ago

1.13.48

3 years ago

1.13.47

3 years ago

1.13.45

3 years ago

1.13.42

3 years ago

1.13.31

3 years ago

1.13.41

3 years ago

1.13.30

3 years ago

1.13.44

3 years ago

1.13.33

3 years ago

1.13.32

3 years ago

1.13.40

3 years ago

1.13.39

3 years ago

1.13.28

3 years ago

1.13.38

3 years ago

1.13.29

3 years ago

1.13.35

3 years ago

1.13.34

3 years ago

1.13.37

3 years ago

1.13.36

3 years ago

1.13.27

3 years ago

1.13.26

3 years ago

1.13.25

3 years ago

1.13.24

3 years ago

1.13.23

3 years ago

1.13.22

3 years ago