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

socket-model

v0.92.10

Published

A small socket network framework to support inter-process JSON messages using unix sockets.

Downloads

124

Readme

Socket Model


A small unix socket framework to support inter-process communications.

NPM version Build Status Dependency Status

Overview

Socket Model is a set of server/client reader/writer objects that provide a framework for inter-process communications based on unix sockets. Communications exchange JSON messages delimited by CR (\n). Messages can be read and written from either client or server using send() and read() where the message is either a string or a complex object. Servers have the ability to broadcast messages to all active clients.

Each message is wrapped in an object with the message id (mid), time stamp (ts) and the message. Messages are parsed by the receiver (either client or server). Message helpers include MessageWriter and MessageReader. These classes may be swapped out or extended to provide additional capabilities. They are injected to SocketServer and SocketClient when invoked through SocketModel's create methods.

For most applications using the server and client provide all thats required to exchange messages. But, since the reader and writer are separate objects injected into the server and client, these objects my be swapped or overriden to provide extra processing including authentication, message digest, priority, etc.

Installation

npm install socket-model --save

Use

// server process

const SocketModel = require('socket-model');

const server = SocketModel.createServer( { socketFile:'/tmp/test.sock' } );

server.onMessage(function(obj) {
	console.log(' <<< Client Message: ', obj.message);
	server.broadcast('echo: ', obj.message);
});

server.start();

// cleient process

const SocketModel = require('socket-model');

const client = SocketModel.createClient( { socketFile:'/tmp/test.sock' } );

client.onMessage(function(obj) {
	console.log(' <<< Server Message: ', obj.message);
});

client.start();
client.send('hello socket model!');

// send more messages, then end the connection with an optional message..
client.stop('bye');

Examples

See the examples folder for server and client message examples.

API

SocketModel

The socket model object contains two factory methods to create server and client objects. They can be invoked with preset options to set socket file, reader, writer, and other options. The client must be created with a specified socket file (see examples).

  • SocketModel.createServer(options)
  • SocketModel.createClient(options)

SocketServer

Once the socket server has be created it can be started to listen for client connections. The broadcast method is used to send a wrapped, stringified JSON message to all clients in the list. If the list is null, then the message is sent to all clients.

Incoming messages can be listened to by assigning a callback to onMessage(). Incoming JSON strings are parsed by the reader then sent as objects to handlers. Message objects have a message id, timestamp, and message payload.

  • server.start()
  • server.broadcast( obj, clientList )
  • server.onMessage( messageHandler )
  • server.getClients()
  • server.onClientConnection( clientConnectionHandler )
  • event: client - fired when a new client connects

SocketClient

One a socket client is created it may be started to attempt connection to the server. If the server is unavailable, the client will continue to attempt to connect (set by options). If a server connection drops, the client will again attempt to connect.

  • client.start()
  • client.send(obj)
  • client.onMessage(callback)
  • client.stop()

MessageWriter

The message writer is responsible for wrapping outgoing messages with a message id, timestamp and the message. The wrapped object is then stringified and sent to the specified client.

  • writer.send(obj, socket)
  • writer.wrapMessage()

MessageReader

The message reader is responsible for parsing the incoming message and firing a 'message' event with the parsed object.

  • reader.lineHandler(callback)
  • event: message - fired when a new messages is received

Copyright © 2014-2016, rain city software | Version 00.92.10