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

json-over-tcp

v0.3.2

Published

TCP server/client messaging in JSON

Downloads

49

Readme

jot (json-over-tcp) Build Status

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.

A Short Illustration

// assume that I have a jot server listening somewhere and I created a connection to it called "connection"
var someObject = {
  "this property is null": null,
  1928: 3734,
  turtle: {
    neck: "sweater"
  }
};

connection.write(someObject);
// 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".

A Real Example

// 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 = 8099,
  jot = require('json-over-tcp');

var server = jot.createServer(someRandomPort);
server.on('listening', createConnection);
server.on('connection', newConnectionHandler);

// Triggered whenever something connects to the server
function newConnectionHandler(socket){
  // Whenever a connection sends us an object...
  socket.on('data', function(data){
    // Output the question property of the client's message to the console
    console.log("Client's question: " + data.question);

    // Wait one second, then write an answer to the client's socket
    setTimeout(function(){
      socket.write({answer: 42});
    }, 1000);
  });
};

// Creates one connection to the server when the server starts listening
function createConnection(){
  // Start a connection to the server
  var socket = jot.connect(someRandomPort, function(){
    // Send the initial message once connected
    socket.write({question: "Hello, world?"});
  });
  
  // Whenever the server sends us an object...
  socket.on('data', function(data){
    // Output the answer property of the server's message to the console
    console.log("Server's answer: " + data.answer);
    
    // Wait one second, then write a question to the socket
    setTimeout(function(){
      // Notice that we "write" a JSON object to the socket
      socket.write({question: "Hello, world?"});
    }, 1000);
  });
}

// Start listening
server.listen(someRandomPort);

API

The factory functions below behave similar to node's net package (but they return jot versions of the server or socket).

jot.createServer([options], [connectionListener])

See net.createServer([options], [connectionListener]).

jot.connect(options, [connectionListener])

See net.connect(options, [connectionListener]).

jot.createConnection(options, [connectionListener])

See net.createConnection(options, [connectionListener]).

jot.connect(port, [host], [connectListener])

See net.connect(port, [host], [connectListener]).

jot.createConnection(options, [connectionListener])

See net.createConnection(port, [host], [connectListener]).

jot.connect(path, [connectListener])

See net.connect(path, [connectListener]).

jot.createConnection(path, [connectListener])

See net.createConnection(path, [connectListener]).

jot.createProtocol(stream)

Factory function for creating a jot protocol object.

jot.createSocket([options])

Factory function for creating a jot socket.

jot.Server

The server API is the same as the Server API in the native 'net' module with the following differences:

Event: 'connection'

Emits a jot socket (see it's API below) instead of a plain tcp socket.

jot.Socket

The socket API is the same as the Socket API in the native 'net' module with the following differences:

Event: 'data'

Emits a JSON object which was sent by the other end of the socket.

write(obj)

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.

jot.Protocol

The protocol object is what serializes/deserializes JSON data over the wire.

new Protocol(stream)

Takes in a Stream object and reads/writes JSON objects using it's a simple protocol (a protocol signature, message length, and stringified JSON).

write(obj)

Writes an object which can be stringified to the stream.

on

Bind to an event ('data' is the only one ever emitted).

removeListener

Remove a bound listener.

Event: 'data'

Emits a JSON object whenever a stream message is recieved.

Protocol

If you would like to implement the protocol yourself, the server will expect the following in order in the byte stream:

  1. 16-bit unsigned little-endian integer with 206 as the value. This is the protocol signature, if a message is sent without this signature a protocol error will be raised.
  2. A 32-bit unsigned little-endian integer with the length of the message being sent as the value.
  3. A UTF-8 string with the stringified JSON as the value (the message).