1.0.1 • Published 6 years ago

socketize v1.0.1

Weekly downloads
6
License
MIT
Repository
github
Last release
6 years ago

socketize

Promise based socket communication for Node.js

The Problem

Node.js provides a callback based Socket API in the Net package. Using this API you might end up in the callback hell. Therefore socketize provides a Promise based Socket communication arround the Net API.

Installation

To install the stable version:

npm install --save socketize

if you are using npm as your package manager and

yarn add socketize

for using yarn.

Examples

Simple Unix Socket communication

import Socket from 'socketize';

const socket = new Socket();

socket.connect({path: '/var/run/my.unix.socket'})
  .then(() => socket.write('Hello world'));
  .then(() => socket.read())
  // remote server responds with '42'
  .then(answer => console.log('The answer is' answer));
  // 'The answer is 42'
  .catch(err => console.log(err));

Stateful reading

import Socket from 'socketize';

const createReader = () => {
  const end = false;
  return {
    read: (data = '', chunk) => {
      // read until 'END' is contained in the response
      const readdata = data + chunk;
      if (readdata.endsWith('END')) {
        end = true;
        return data += chunk.slice(0, -3).trim();
      }
      return readdata;
    };
    isDone: () => end,
  };
};

const reader = createReader();
const socket = new Socket();

socket.connect({path: '/var/run/my.unix.socket'})
  .then(() => socket.write('Hello world'));
  .then(() => socket.read(reader.read, reader.isDone));
  // remote server responds with '42 END'
  .then(answer => console.log('The answer is' answer));
  // 'The answer is 42'

License

Licensed under the MIT License, Copyright © 2018 Björn Ricks

See LICENSE for details.

1.0.1

6 years ago

1.0.0

6 years ago