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 🙏

© 2025 – Pkg Stats / Ryan Hefner

socket-wrapper

v1.0.2

Published

Send and receive separate messages via socket stream

Readme

SocketWrapper

Send and receive separate messages via socket stream

Installation

npm install --save socket-wrapper

Usage

As you know TCP socket is a stream: order of bytes is guaranteed but there is no idea of messages just a stream of bytes. You can send several separate chunks of data and receive it on the other end with a single read. Or you can send one big chunk of data but receive it with several reads.

This class will allow you to send and receive distinct messages. The messages will not be combined or split. It is achieved by sending message size as UINT32 big-endian integer before the actual data. Receiving side will read first 4 bytes, convert it to the number of bytes and will be reading the stream until all the bytes are read.

const SocketWrapper = require('socket-wrapper');

let wrapper = new SocketWrapper(socket); // feed it a nodejs tcp socket
wrapper.on('receive', message => {
  console.log(message.toString()) // receive the message as a Buffer
});
wrapper.send(Buffer.from('hello world')); // send any Buffer

SocketWrapper

new SocketWrapper([socket])

Creates an instance. socket is an optional socket instance this wrapper will attach to

.incoming

Incoming buffer. Contains data received so far. When data is received from the socket 'read' event is fired and it is appended to this buffer for parsing messages.

When there is enough data to extract the message then 'receive' event will be fired and the buffer will be shortened by emitted data.

.outgoing

Outgoing buffer. Temporary storage for the data scheduled to be sent. Each act of sending the data is followed by 'write' event. When the buffer is depleted 'flush' event is fired.

.isAttached

Whether this wrapper is attached to a socket or not.

.attach(socket)

Attach this wrapper to a socket. Sending/receiving messages becomes available.

.detach()

Detach from the socket

.send(buffer)

Send a Buffer as a message. SocketWrapper on the other side will receive this exact buffer.

.clear()

Clear incoming and outgoing buffers.

.destroy()

Detach and delete all the internal data.

event: read

Fired when new data has arrived via the socket

event: write

Fired when current outgoing buffer is written to the socket

event: flush

Fired when outgoing buffer is depleted

event: receive

Fired when new message is completely received

event: send

Fired when new chunk is appended to the outgoing buffer