Wondering what’s next for npm?Check out our public roadmap! »

    socket-jet
    TypeScript icon, indicating that this package has built-in type declarations

    0.2.5 • Public • Published

    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.

    Keywords

    none

    Install

    npm i socket-jet

    DownloadsWeekly Downloads

    8

    Version

    0.2.5

    License

    MIT

    Unpacked Size

    40.9 kB

    Total Files

    20

    Last publish

    Collaborators

    • avatar