Code Monkey home page Code Monkey logo

jotan's Introduction

Playing with sending framed data over tcp sockets. Because udp in node is slow.

UPDATE: the benchmarks say that udp is faster than framed tcp. Feel free to run them (see bench/) to see for yourself.


jotans‍trings o‍ver t‍cp a‍nd n‍etstrings

This is a thin wrapper around the netstrings module which makes it easy to send strings & buffers over tcp.

If you feel like sending json you can do that, all you need to do is add a line to stringify and a line to parse, as you can see in tests/json.js.

install

$ npm install jotan --save

client

how to connect to a server

  var j = jotan(PORT, HOST)
  j.send(new Buffer('café'))
  j.send('c')

  setTimeout(function() {
    j.send('life rocks!')

    // fails due to default 1000ms timeout on the server
    setTimeout(function() {
      j.send('life rocks!')
      j.end()
    }, 1100)

  }, 500)

server

One .send from the client results in one data event on the server's messageStream.

Where possible the API mimics that of the net module.

See tests/strings-and-buffers.js for a working example.

  jotan.createServer(onConnection, { port: PORT })

  function onConnection(rawSocket, messageStream) {
    messageStream.on('data', function(buf) {
      as.equal('object', typeof buf)
      var str = buf.toString('utf8')
      console.log('server>', str, '?', MSGS[msgsReceived])
    })
  }

todos

  • client
    • reconnect
    • emit errors
    • chainable
    • pipeable
    • socket.setKeepAlive
  • server
    • what to do when client sends bad data
    • if payload is too big, as dictated by the prepended length, drop it to prevent ddos
    • emit errors
    • chainable
    • drop data instead of backpressure
  • tests!
    • performance / stress
    • good values for server timeout

MIT License

jotan's People

Contributors

dtrejo avatar

Stargazers

 avatar

Watchers

 avatar James Cloos avatar  avatar

jotan's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.