{"library":"node-unix-socket","title":"Node.js Unix Socket Extensions","description":"node-unix-socket is a Node.js addon, built with napi-rs and leveraging libuv, that extends Node.js's native networking capabilities to support Unix `SOCK_SEQPACKET` and `SOCK_DGRAM` sockets. It also enables the use of `SO_REUSEPORT` for TCP `net.Server` instances, offering an alternative to Node.js's built-in `cluster` module for load balancing, with kernel-level distribution. The package is currently at version 0.2.7, indicating ongoing development. It differentiates itself by providing these advanced socket types without introducing additional asynchronous runtimes, relying solely on Node.js's internal libuv. Pre-compiled binaries are shipped for common platforms, reducing the need for compilation environments. This library is particularly useful for inter-process communication patterns requiring message boundary preservation (seqpacket) or connectionless datagram communication, as well as optimizing high-throughput TCP servers. While release cadence isn't explicitly stated, its focus on specific, low-level features suggests a stable, less frequent update cycle unless major Node.js changes necessitate it.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install node-unix-socket"],"cli":null},"imports":["import { SeqpacketServer } from 'node-unix-socket';","import { SeqpacketSocket } from 'node-unix-socket';","import { DgramSocket } from 'node-unix-socket';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { SeqpacketServer, SeqpacketSocket } from 'node-unix-socket';\nimport os from 'os';\nimport path from 'path';\nimport fs from 'fs';\n\nconst bindPath = path.resolve(os.tmpdir(), './my_seqpacket.sock');\n\n// Ensure the socket file does not exist from previous runs\ntry {\n  fs.unlinkSync(bindPath);\n} catch (e) {\n  // Ignore if file does not exist\n}\n\n// Create and start a Seqpacket server\nconst server = new SeqpacketServer();\nserver.listen(bindPath);\nserver.on('connection', (socket) => {\n  console.log('Server: Client connected');\n  socket.on('data', (buf) => {\n    console.log('Server: received', buf.toString());\n  });\n  socket.on('end', () => {\n    console.log('Server: Client disconnected');\n  });\n});\nserver.on('listening', () => {\n  console.log(`Server listening on ${bindPath}`);\n});\nserver.on('error', (err) => {\n  console.error('Server error:', err);\n});\n\n// Create and connect a Seqpacket client\nconst client = new SeqpacketSocket();\nclient.connect(bindPath, () => {\n  console.log('Client: Connected to server');\n  const data = ['hello, ', 'w', 'o', 'r', 'l', 'd'];\n\n  for (const str of data) {\n    client.write(Buffer.from(str));\n    console.log(`Client: Sent '${str}'`);\n  }\n  client.end(() => {\n    console.log('Client: Disconnected, closing server in 1 sec...');\n    setTimeout(() => server.close(), 1000);\n  });\n});\nclient.on('error', (err) => {\n  console.error('Client error:', err);\n  server.close(); // Ensure server closes on client error\n});\n\n// Clean up on process exit\nprocess.on('exit', () => {\n  try {\n    fs.unlinkSync(bindPath);\n    console.log('Cleaned up socket file.');\n  } catch (e) {\n    // Ignore if file already removed or never created\n  }\n});","lang":"typescript","description":"This example demonstrates how to set up a SeqpacketServer and connect a SeqpacketSocket client to it, sending multiple messages while preserving message boundaries, then cleaning up the socket file.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}