A node.js client and server api for bidirectional sending of json over tls after exchanging a password.
When using a
reconnectInterval on the client end of the api, clients will reconnect to a server if a connection is lost.
npm install tls-json
openssl genrsa -out server-key.pem 2048 openssl req -new -key server-key.pem -out server-csr.pem openssl x509 -req -in server-csr.pem -signkey server-key.pem -out server-cert.pem
Generate localhost cert
# a dev env ~10 year cert, such as the one used in the example code below openssl req -x509 -newkey rsa:2048 -sha256 -nodes -keyout localhost.key -out localhost.crt -subj "/CN=localhost" -days 3650
The api is essentially the following events:
request. Send will send a message, without any acknowledgement needed. Request sends a message and returns a promise that will resolve to the response -- in other words requests are acknowledged and the sender can be sure that they were received. A simplified
response pattern is used, where the
req contains the data sent, and the
res can be used to reply.
Client and server have essentially the same api for communication, with main difference being that the outgoing server calls take a client
id as an argument (that's who the message goes to). The client on the other hand invokes
request without an
id because all of its messages can only go to the server.
New to version 3.2+
A keepalive ping/pong have been added (works automatically, no api changes, but configurable if desired).
const fs =const TLSServer = Serverconst port = 8888const server =// this is a tls options object, seeoptions:key: fscert: fsrejectUnauthorized: truerequestTimeout: 10000 // milliseconds until a request is considered timedoutkeepAliveInterval: 10000 // milliseconds interval to check if the socket is alivekeepAliveTimeout: 5000 // milliseconds until a socket is considered dead if it hasn't respondedpassword: 'this string is a password, change it'// client connected and supplied password correctlyserver// client closed connection// gauranteed to fire if a client disconnects, timeouts, etcserver// client sent a messageserver// client sent a requestserver// note: close will always fire if there is a problem, so error and// timeout are merely for information/debuggingserverserverserver/* outgoing examples */// NOTE: to send anything we just refer to the client by idserverserver// ortryconst data = await servercatch err// and of course responding to requests is outgoing as well
Clients are assigned an id when they connect, and their id is the first arg for all events. This id can be used to send messages and requests to the clients..
const fs =const TLSClient = Clientconst client =// see: for tls optionsoptions:ca: fs // example allows self-signed certshost: 'localhost'port: 8888reconnectInterval: 5000 // milliseocnds required! defaults to 0 which is no reconnectingrequestTimeout: 10000password: 'this string is a password, change it'clientclientclientclient// informationalclientclient/* outgoing */// NOTE: no id needed, the client only sends to the serverclientclient//ortryconst data = await clientcatch err// and of course responding to requests is outgoing as well
Clients with a reconnectInterval > 0 will automatically attempt to reconnect to a server after losing connection. Clients will not automatically send messages that failed to send prior to losing connection (this may be done manually if desired, but be careful about the volume).
These are errors that can come through the error eventer handler, or through the request promises
- any socket errors such as
- 'not connected or not authenticated' - when using
sendthis is emitted if the client or server has not yet authenticated or is not connected, when using
requestthis same error will come through the promise
request timeout- when no response comes back within
requestTimeout, most likely caused by forgetting to use
connection lost- rare, but can occur if a request is made but the connection is lost before the other service can respond
For spam reasons when a client is in reconnect mode the
ECONNREFUSED errors and socket
close errors are suppressed. If you'd like to log how often reconnect attempts occur anyways, listen for