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 🙏

© 2026 – Pkg Stats / Ryan Hefner

hl7v2-net

v1.9.0

Published

HL7 v2 server/client for NodeJS

Readme

hl7v2-net

NPM Version NPM Downloads CI Tests Test Coverage

About

HL7 v2.x MLLP server and client for Node.js. This package provides a high-level API for building HL7 servers and clients using TCP or TLS.

Installation

$ npm install hl7v2-net --save

Usage Example

Server Example

import { HL7Server } from 'hl7v2-net';

const server = HL7Server.createServer();

// Middleware to handle incoming messages
server.use(async (req, res) => {
  console.log('Received message:', req.message.toHL7String());
  // res.message is already initialized with an ACK
  // You can modify it or just let the server send it automatically
});

server.listen(12345, () => {
  console.log('HL7 Server listening on port 12345');
});

Client Example

import { Hl7Client } from 'hl7v2-net';
import { HL7Message } from 'hl7v2';

const client = Hl7Client.createClient({ host: 'localhost', port: 12345 });

async function send() {
  await client.connect();
  
  const msg = new HL7Message();
  msg.header.field(9).setValue('ORU^R01');
  msg.addSegment('PID').field(5).setValue('DOE^JOHN');
  
  const response = await client.sendMessageWaitAck(msg);
  console.log('Received ACK:', response.toHL7String());
  
  await client.close();
}

send().catch(console.error);

API Reference

HL7Server

The HL7Server class is used to create an HL7 MLLP server.

Methods

.createServer()

Creates a new HL7Server instance for TCP connections. static createServer(listenerOptions?: net.ListenOptions, options?: HL7Server.Options): HL7Server example

const server = HL7Server.createServer();
.createTlsServer()

Creates a new HL7Server instance for TLS connections. static createTlsServer(listenerOptions?: tls.TlsOptions, options?: HL7Server.Options): HL7Server example

const server = HL7Server.createTlsServer({
  key: fs.readFileSync('server-key.pem'),
  cert: fs.readFileSync('server-cert.pem')
});
.listen()

Starts the server listening for connections. listen(port?: number, hostname?: string, backlog?: number): Promise<void> example

await server.listen(12345);
.close()

Stops the server from accepting new connections and closes existing ones. close(waitRunningHandlers?: number): Promise<void> example

await server.close(5000); // Wait up to 5s for running handlers
.use()

Adds a middleware handler or another router to the server. use(handler: HL7Middleware | HL7Router, priority?: number): void example

server.use((req, res, next) => {
  // Do something
  next();
});
.onError()

Adds an error middleware handler or another router to the server. onError(handler: HL7ErrorMiddleware | HL7Router, priority?: number): void example

server.onError((err, req, res, next) => {
  console.error(err);
  res.failed(err);
  next();
});

Hl7Client

The Hl7Client class is used to create an HL7 MLLP client.

Properties

  • connected: boolean: Whether the client is currently connected.
  • readyState: string: The state of the underlying socket.

Methods

.createClient()

Creates a new Hl7Client instance for TCP connections. static createClient(options: Hl7Client.NetConnectOptions): Hl7Client example

const client = Hl7Client.createClient({ host: 'localhost', port: 12345 });
.createTlsClient()

Creates a new Hl7Client instance for TLS connections. static createTlsClient(options: Hl7Client.TlsConnectOptions): Hl7Client example

const client = Hl7Client.createTlsClient({ host: 'localhost', port: 12345, rejectUnauthorized: false });
.connect()

Connects to the server. connect(): Promise<void> example

await client.connect();
.close()

Closes the connection. close(): Promise<void> example

await client.close();
.sendMessage()

Sends an HL7 message without waiting for an acknowledgment. sendMessage(message: HL7Message | string): Promise<void> example

await client.sendMessage(msg);
.sendMessageWaitAck()

Sends an HL7 message and waits for an acknowledgment. sendMessageWaitAck(message: HL7Message | string): Promise<HL7Message> example

const ack = await client.sendMessageWaitAck(msg);

HL7Socket

Represents an HL7 MLLP socket connection.

Properties

  • connected: boolean: Whether the socket is connected.
  • remoteAddress: string: The remote IP address.
  • remotePort: number: The remote port.

Methods

.sendMessage()

Sends an HL7 message. sendMessage(message: HL7Message | string): Promise<void> example

await socket.sendMessage(msg);
.sendMessageWaitAck()

Sends an HL7 message and waits for an acknowledgment. sendMessageWaitAck(message: HL7Message | string): Promise<HL7Message> example

const ack = await socket.sendMessageWaitAck(msg);

HL7Router

Provides routing capabilities for HL7 messages based on middleware.

Methods

.use()

Adds a middleware or another router to this router. use(handler: HL7Middleware | HL7Router, priority?: number): void example

const router = new HL7Router();
router.use((req, res) => { ... });
server.use(router);
.onError()

Adds an error middleware or another router to this router. onError(handler: HL7ErrorMiddleware | HL7Router, priority?: number): void example

const router = new HL7Router();
router.onError((err, req, res, next) => { ... });
server.use(router);

HL7Request

Represents an incoming HL7 request.

Properties

  • socket: HL7Socket: The socket the request was received on.
  • message: HL7Message: The received HL7 message.

HL7Response

Represents the response to an HL7 request.

Properties

  • request: HL7Request: The corresponding request.
  • message: HL7Message: The HL7 message to be sent as a response (defaults to an ACK).
  • finished: boolean: Whether the response has been sent.

Methods

.failed()

Marks the request as failed and sets the response to a NAK message. failed(error: Error): void example

res.failed(new Error('Processing failed'));

Node Compatibility

  • node >= 20.x

License

hl7v2-net is available under MIT license.