TCP server/client messaging in JSON
Node.js TCP server/client messaging in JSON.
This library provides a server and a socket class (with APIs that are very similar to the net package in Node.js) which communicate by sending each other JSON messages.
You "write" JSON objects to the socket, and the "data" events on the other end of the socket emits the JSON object you wrote.
// assume that I have a jot server listening somewhere and I created a connection to it called "connection"var someObject ="this property is null": null1928: 3734turtle:neck: "sweater";connectionwritesomeObject;// Whatever is listening to this connection on the server-side will now recieve a "data" event with an object that// has the same values as "someObject".
// This script will output "Client's question: Hello, world?" and "Server's answer: 42" in alternating order// every second until the script is stopped.var someRandomPort = 8099jot = require'json-over-tcp';var server = jotcreateServersomeRandomPort;serveron'listening' createConnection;serveron'connection' newConnectionHandler;// Triggered whenever something connects to the server// Whenever a connection sends us an object...socketon'data'// Output the question property of the client's message to the consoleconsole.log"Client's question: " + dataquestion;// Wait one second, then write an answer to the client's socketsetTimeoutsocketwriteanswer: 42;1000;;;// Creates one connection to the server when the server starts listening// Start a connection to the servervar socket = jotconnectsomeRandomPort// Send the initial message once connectedsocketwritequestion: "Hello, world?";;// Whenever the server sends us an object...socketon'data'// Output the answer property of the server's message to the consoleconsole.log"Server's answer: " + dataanswer;// Wait one second, then write a question to the socketsetTimeout// Notice that we "write" a JSON object to the socketsocketwritequestion: "Hello, world?";1000;;// Start listeningserverlistensomeRandomPort;
The factory functions below behave similar to node's net package (but they return jot versions of the server or socket).
Factory function for creating a jot protocol object.
Factory function for creating a jot socket.
The server API is the same as the
ServerAPI in the native 'net' module with the following differences:
Emits a jot socket (see it's API below) instead of a plain tcp socket.
The socket API is the same as the
SocketAPI in the native 'net' module with the following differences:
Emits a JSON object which was sent by the other end of the socket.
Sends an object to the other end of the socket. This method doesn't accept any of the other parameters as the plain tcp socket.
The protocol object is what serializes/deserializes JSON data over the wire.
Takes in a
Stream object and reads/writes JSON objects using it's a simple protocol (a protocol signature, message length, and stringified JSON).
Writes an object which can be stringified to the stream.
Bind to an event (
'data' is the only one ever emitted).
Remove a bound listener.
Emits a JSON object whenever a stream message is recieved.
If you would like to implement the protocol yourself, the server will expect the following in order in the byte stream: