Giter VIP home page Giter VIP logo

sync-socket's Introduction

SyncSocket

This Node.js module exposes a no-dependency class called SyncSocket, which acts as a synchronous interface to Node's net.Socket instance, which is normally only asynchronous.

This module exposes many of the same methods and properties a normal net.Socket has.

Considerations

SyncSocket is slow. Not unusably slow, but you should have a good reason to want to use it. This was made for the rare case you need blocking read/writes in your Node.js application.

In addition, another one of my modules, netlinkwrapper exposes a similar synchronous socket. However that module requires node-gyp to compile. SyncSocket does not require node-gyp. In fact, it has no dependencies.

How Does it Work?

tl;dr: I abuse child_process.execFileSync.

I've done some pretty thorough research into how Node's net.Socket module works, and you can't make it synchronous without rewriting it in C++ via node-gyp, or using some modules like fibers that in turn would depend on node-gyp anyways.

With that in mind I've devised this hack that abuses child processes.

One process, the worker, is spawned that uses the normal net module doing read/writes asynchronously as you should to net.Sockets. It then creates a TCP server to allow requests to its net.Socket.

Then another file, the query script, acts as a script that will request something from the worker, and spit the result out to stdout once it has the results, again gotten asynchronously.

Now from our main thread we can abuse child_process.execFileSync, which executes the query script, which then queries our worker process, and exits with the result.

Obviously this is a ton of overhead just to avoid callbacks. But it works.

Benchmarks

I've ran some tests, and SyncSocket is about 20 times slower in doing read/writes than my competing module netlinkwrapper. So if node-gyp is not an issue to you, use that instead. Or really just use net.Socket.

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.