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-post-message

v1.0.3

Published

Do the `postMessage()`/`on('message')` thing with any arbitrary socket (really, any stream) in Node.js.

Downloads

11,752

Readme

socket-post-message

Do the postMessage()/on('message') thing with any arbitrary socket (really, any stream) in Node.js.

What this is for

Say you have a socket, like a server/client connected on a domain socket or over a TCP connection, and you want to send and receive arbitrary messages.

This lets you do that.

How it works

Messages are encoded using the serialize and deserialize methods from node:v8, and adding a 4 byte header for the message length.

This supports many things that are not supported by JSON.stringify/parse, like cyclical objects and native JS object types, but is limited to things that are serializable, so for example, you can't pass a function through the stream and expect it to remain intact.

If the stream emits data that is not parseable, doesn't have a correct message header, etc., then the returned messageHost object will emit an 'error' event.

In almost all cases, this is done with zero copying. (There is a small copy when the message is split across more than 2 chunks, but that's pretty rare.)

USAGE

Install it with npm

npm install socket-post-message

In the server:

import { createServer } from 'node:net'
import { socketPostMessage } from 'socket-post-message'

const server = createServer(connection => {
  const messageHost = socketPostMessage(connection)

  messageHost.on('message', msg => {
    messageHost.postMessage(['received', msg])
  })

  messageHost.postMessage({ status: 'connected' })
})

socket.listen('socket-name')

In the client:

import { connect } from 'node:net'
import { socketPostMessage } from 'socket-post-message'

const socket = connect('socket-name')
const messageHost = socketPostMessage(socket)

messageHost.on('message', msg => console.log(msg))

messageHost.once('connect', () => {
  messageHost.postMessage({ hello: 'world' })
})

// outputs:
// { status: 'connected' }
// [ 'received', { hello: 'world' } ]

Caveats

You can't send file descriptors over the message channel, because that is not supported in Node for sockets. (It was once upon a time supported for domain sockets, but that's not supported by Windows named pipes, and so was removed in Node v0.6 when Windows support was added.) So, the transferList feature is not supported.

If you use the stream for any other kind of data, it's going to get weird. You really need to ensure that both sides of the channel stream are using this library (or another "UInt32BE message length header + v8 serialized message" implementation) to send and receive messages. Because basically any series of 4 bytes can be interpreted as a number, if you write something like "hello, world" to the stream, then it will be interpreted as a message length header of 0x68656c6c, and keep reading until it has consumed 1751477356 bytes. Probably not what you want.

If you do have a message like '\0\0\0\x0chello world', then the reader will consume the 12 bytes of 'hello world' and then try to deserialize it, and that will fail, emitting an error on the messageHost.

If communicating across threads in Node, or worker/forked processes that you control, it's usually better to use either a MessageChannel pair or an IPC stdio file descriptor, as this has more features and an easier setup.

But if you need to efficiently send and receive arbitrary messages between arbitrary processes, then this is a good choice.