Synket
Synket allow synchronous sockets. The flow of operations is as follows:
-
Client sends a message to the socket
-
Client is paused
-
Server receives the message
-
Server does some processing that can take any length of time
-
Server sends data back to Client
-
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 = ; var server = net; /*//Alternatively listen on a URL and port: }).listen({url: 'http://example.org', port: 4564}); */
Now the client:
const Synket = ; //start the socketvar connection = 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 hostvar result = connection; console;
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
:
moduleexports = class { ; }
And then launch it from a node script (this can be the client or another script):
const Synket = ; //start the socketvar connection = 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 upconnection;
The server will now be launched in its own thread and you can send messages to it like so:
var result = connection; console;
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.