2.0.4 • Published 2 months ago

@ima/plugin-websocket v2.0.4

Weekly downloads
5
License
MIT
Repository
github
Last release
2 months ago

@ima/plugin-websocket

The IMA plugin allow creating socket server and connected clients can send broadcast message. The plugin is used by default in @ima/gulp-tasks which creates one socket server.

Installation

npm install @ima/plugin-websocket --save
// /app/build.js

// For only dev environment
if (
  process.env.NODE_ENV === 'dev' ||
  process.env.NODE_ENV === 'development' ||
  process.env.NODE_ENV === undefined
) {
  vendors.common.push('@ima/plugin-websocket');
}

// or for all environments
// let vendors = {
//     common: [
//         '@ima/plugin-websocket'
//     ]
// };

/*
The websocket plugin is now available.

import { WebSocket } from '@ima/plugin-websocket';
*/

Usage

Creating client

The below example is for client side usage.

// /some/extra/Logic.js
import { WebSocket } from '@ima/plugin-websocket';

class Logic {
  /** @type {import('@ima/core').Dependencies} */
  static get $dependencies() {
    return [WebSocket];
  }

  constructor(webSocket) {
    this._webSocket = webSocket;

    this._listener = (data) => {
      if (data.sentinel === 'some-ID') {
        console.log(data.payload);
      }
    };
  }

  sendMessageToAllClients() {
    this._webSocket.send({
      sentinel: 'some-ID',
      payload: {}
    });
  }

  listenOnMessageFromServer() {
    this._webSocket.subscribe(this._listener);
  }

  unlistenOnMessageFromServer() {
    this._webSocket.unsubscribe(this._listener);
  }
}

The below example is for server side usage. The plugin use ws module on the server side so you can use his API.

// /server.js

const { createClient } = require('@ima/plugin-websocket/lib');

// default value for url is 'ws://localhost:5888'
const socket = createClient({ url: 'ws://localhost:5888' });


socket.on('open', () => {
  socket.send(JSON.stringify({ sentinel: 'some-ID', payload: {} }));
});

socket.on('message', (data) => {
  console.log(data);
});

Creating own server

The below example is for creating new broadcast socket server. The plugin use ws module on the server side so you can use his API.

// /server.js
const { createServer } = require('@ima/plugin-websocket/lib');

// you can use ws module options and API
// default value for port is 5888
const socket = createServer({ port: 5888 });

Configuration

The plugin has got only two options. The url option is for address of socket server and debug option is for turn on debug messages from plugin.

// /app/settings.js

const initSettings = () => {
  return {
    prod: {
      plugin: {
        websocket: {
          url: 'ws://localhost:5888',
          debug: false
        }
      }
    }
  };
};

IMA.js

The IMA.js is an application development stack for developing isomorphic applications written in pure JavaScript. You can find the IMA.js skeleton application at https://github.com/seznam/ima.

2.0.4

2 months ago

2.0.3

1 year ago

2.0.2

1 year ago

2.0.2-rc.3

1 year ago

2.0.2-rc.2

1 year ago

2.0.2-rc.5

1 year ago

2.0.2-rc.4

1 year ago

2.0.2-rc.1

1 year ago

2.0.2-rc.0

1 year ago

2.0.0-rc.10

1 year ago

2.0.0-rc.7

2 years ago

2.0.1

1 year ago

2.0.0-rc.8

2 years ago

2.0.0

1 year ago

2.0.0-rc.9

2 years ago

2.0.0-rc.3

2 years ago

2.0.0-rc.4

2 years ago

2.0.0-rc.5

2 years ago

2.0.0-rc.6

2 years ago

2.0.0-rc.2

2 years ago

2.0.0-rc.0

2 years ago

2.0.0-rc.1

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.0

3 years ago

1.0.2

3 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago