npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

synket

v1.0.3

Published

Synchronous sockets for node

Downloads

241

Readme

Synket

Synket allow synchronous sockets. The flow of operations is as follows:

  1. Client sends a message to the socket

  2. Client is paused

  3. Server receives the message

  4. Server does some processing that can take any length of time

  5. Server sends data back to Client

  6. Client is resumed and the return value of the the send command is the data that was written to the socket by Server

Firstly, a basic server server running on another thread on the same machine or another machine:

const net = require('net');

var server = net.createServer(function(sock) {
	socket.setEncoding('utf8');

	//Receive data from client
	sock.on('data', function(data) {

		///Do something that takes time:
		
		setTimeout(function() {
			sock.write('The client sent: ' + data);
		}, 3000);
	});

	
}).listen({path: './test.sock'});

/*
//Alternatively listen on a URL and port:

}).listen({url: 'http://example.org', port: 4564});

*/

Now the client:

const Synket = require('synket');

//start the socket
var connection = new Synket({path: './test.sock'});

/*
//Alternatively use a URL and port:

var connection = new Synket({url: 'http://example.org', port: 4564});
*/

//send a message, will not return until data is written to the socket by the host
var result = connection.send('doSomething');

console.log(result);

When this code runs it pause for 3 seconds then print The client sent: doSomething.

Quick Server

Synket also provides a mechanism for launching a server that requires less bootstrap code.

You can write a server as node module that returns a class with methods e.g. MyServer.js:


module.exports = class {
	doSomething(socket, args) {
		setTimeout(function() {
			setTimeout(function() {
				socket.write('doSomething called with ' + args[0] + ' and ' + args[1]);
			}, 3000);
		});
	}
}

And then launch it from a node script (this can be the client or another script):

const Synket = require('synket');

//start the socket
var connection = new Synket({path: './test.sock'});

/*
//Alternatively use a URL and port:

var connection = new Synket({url: 'http://example.org', port: 4564});
*/


//Start the server stored in `MyServer.js` this will pause execution until the server is up
connection.startServer('./MyServer.js');

The server will now be launched in its own thread and you can send messages to it like so:

var result = connection.send('doSomething', ['foo', 'bar']);

console.log(result);

This will print doSomething called with foo and bar.

FAQ

Does blocking cause a large overhead?

It depends on your use-case. Synket was designed for use in an Electron application, it should not be used on a production webserver as it will severely limit the number of concurrent requests your sever can handle.