0.2.24 • Published 2 years ago

socket-jet v0.2.24

Weekly downloads
4
License
MIT
Repository
github
Last release
2 years ago

NPM Package Build Status

Socket Jet

Minimalist package for data packets over socket connections.

Features

Implemented

  • Jet#send() returns a promise that will wait for ack.
  • PowerJet#call() to call methods defined on a remote PowerJet.
  • Encryption support based on Node.js Cipher and Decipher.

Installation

yarn add socket-jet
# or
npm install socket-jet --save

Usage

Basic Jet

Server

let server = Net.createServer(socket => {
  let jet = new Jet(socket);

  jet.on('data', data => {
    console.log(data); // {thank: 'you'} from client.
  });

  jet.send('hello').then(
    () => console.log('"hello" sent'),
    error => console.error(error),
  );
});

server.listen(10047);

Client

let socket = Net.connect(10047, () => {
  let jet = new Jet(socket);

  jet.on('data', data => {
    console.log(data); // 'hello' from server.
  });

  jet.send({thank: 'you'}).then(
    () => console.log('data sent'),
    error => console.error(error),
  );
});

Power Jet

Server

class ServerPowerJet extends PowerJet {
  async test(timeout: number): Promise<string> {
    return await new Promise<string>(resolve => {
      setTimeout(resolve, timeout, 'hello, jet!');
    });
  }
}

let server = Net.createServer(socket => {
  new ServerPowerJet(socket);
});

server.listen(10047);

Client

let socket = Net.connect(10047, () => {
  // Note: Client can also have derived `PowerJet` with methods to be called by server.
  let jet = new PowerJet(socket);

  jet.call('test', 1000).then(console.log); // 'hello, jet!' after 1000ms.
});

Encrypted Jet

Server

let server = Net.createServer(socket => {
  let jet = new Jet(socket, {
    crypto: {
      algorithm: 'aes-256-cfb',
      password: 'some password',
    },
  });

  jet.on('data', data => {
    console.log(data); // {thank: 'you'} from client.
  });

  jet.send('hello').then(
    () => console.log('"hello" sent'),
    error => console.error(error),
  );
});

server.listen(10047);

Client

let socket = Net.connect(10047, () => {
  let jet = new Jet(socket, {
    crypto: {
      algorithm: 'aes-256-cfb',
      password: 'some password',
    },
  });

  jet.on('data', data => {
    console.log(data); // 'hello' from server.
  });

  jet.send({thank: 'you'}).then(
    () => console.log('data sent'),
    error => console.error(error),
  );
});

License

MIT License.

0.2.24

2 years ago

0.2.23

2 years ago

0.2.22

2 years ago

0.2.21

2 years ago

0.2.20

2 years ago

0.2.19

2 years ago

0.2.18

2 years ago

0.2.17

2 years ago

0.2.16

2 years ago

0.2.15

2 years ago

0.2.14

2 years ago

0.2.13

2 years ago

0.2.12

2 years ago

0.2.11

2 years ago

0.2.10

2 years ago

0.2.7

2 years ago

0.2.6

2 years ago

0.2.9

2 years ago

0.2.8

2 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.2.3

4 years ago

0.2.2

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago