Skip to content
Recipes

Receive a non-HL7 feed over raw TCP

Take records a device or legacy system sends over plain TCP with its own start and end bytes, and write each one to FHIR as an Observation.

On this page

Point-of-care analyzers, bedside devices and older interface engines often send results over a plain TCP socket rather than MLLP: each record is wrapped in a start byte and an end byte, such as STX (0x02) and ETX (0x03), and the content is not HL7. Mirth calls this a TCP Listener in Basic TCP mode. This recipe declares a raw TCP listener for such a feed, turns each record into a FHIR Observation, and answers the device in its own framing.

Source and destination

The MedHook listener holds the device's connection. Each framed record becomes one run, recorded before the device gets its answer.

flowchart LR
    A[Glucose meter dock] -->|STX record ETX| B[Raw TCP listener]
    B -->|one run per record| C[JavaScript step]
    C --> D[Data map]
    D -->|Observation| E[FHIR R4 server]
    B -->|STX AA ETX| A
  • Source: a device or system that sends text records framed with start and end bytes over TCP.
  • Destination: any FHIR R4 server a FHIR REST profile can reach.
  • Result: one Observation per record. The device gets AA once the record is recorded, AE when it should resend, AR when nothing takes it.

What it uses

The listener

The listener's framing belongs to its port, so your operator declares it in Helm. Quote the bytes:

yaml
mllp:
  listeners:
    - name: glucose-raw
      port: 7001
      framing: frame
      startBytes: "02" # STX
      endBytes: "03" # ETX
      maxConnections: 10 # set it on every raw listener
      idleTimeoutMs: 300000
      frameTimeoutMs: 30000 # a record must arrive within 30 s

A device that closes the connection after each record, with no end byte, uses framing: eos instead; if it keeps a connection open longer than frameTimeoutMs (default twice idleTimeoutMs) before closing it, raise frameTimeoutMs. Under Docker Compose rather than Helm, set MLLP_LISTENERS in .env so the engine and mllp-server both see it, or every record is refused and resent. Keep the port reachable only from the device network (mllp.networkPolicy.allowedCidrs, loadBalancerSourceRanges, or a VPN): the listener does not authenticate senders. Records over MLLP_MAX_FRAME_BYTES (10 MiB) close the connection, and nothing from them is recorded.

The trigger

The trigger takes everything on that listener. Leave messageTypes empty: a record that is not HL7 has no message type.

json
{
  "type": "mllp",
  "listener": "glucose-raw",
  "messageTypes": [],
  "charset": "iso-8859-1"
}

Each run's input carries the record as text:

json
{
  "triggerType": "mllp",
  "triggerId": "trg_glucose",
  "content": "raw",
  "rawMessage": "DEV=DEMO-GLU-01|PID=TP-0001|TIME=20261024T081500|GLU=97|UNIT=mg/dL",
  "messageType": ""
}

Set binary: true on the trigger when the records are binary: rawMessage is then the bytes as base64. A record with a NUL byte arrives that way even without it.

Parse the record

The JavaScript step splits the record into fields:

javascript
const fields = {};
for (const part of input.rawMessage.split("|")) {
  const at = part.indexOf("=");
  if (at > 0) fields[part.slice(0, at)] = part.slice(at + 1);
}
const t = fields.TIME || "";
return {
  status: "final",
  // LOINC 2339-0: Glucose [Mass/volume] in Blood
  loincSystem: "http://loinc.org",
  loinc: "2339-0",
  device: fields.DEV,
  patientId: fields.PID,
  value: Number(fields.GLU),
  unit: fields.UNIT,
  effective: `${t.slice(0, 4)}-${t.slice(4, 6)}-${t.slice(6, 8)}T${t.slice(9, 11)}:${t.slice(11, 13)}:${t.slice(13, 15)}Z`,
};

Map and send

The Data Map builds the Observation:

SourceTarget path
$.statusstatus
$.loincSystemcode.coding[0].system
$.loinccode.coding[0].code
$.patientIdsubject.identifier.value
$.valuevalueQuantity.value
$.unitvalueQuantity.unit
$.effectiveeffectiveDateTime
$.devicedevice.identifier.value

The send step's config is:

json
{ "resourceType": "Observation" }

A test record needs only synthetic values. With printf, \002 and \003 are STX and ETX:

bash
printf '\002DEV=DEMO-GLU-01|PID=TP-0001|TIME=20261024T081500|GLU=97|UNIT=mg/dL\003' \
  | nc -q 2 medhook-mllp.example.test 7001

The listener answers \002AA\003.

Status

Available on this release.

Raw data has no message control id, so it has no dedup key: a device that resends a record after an AE, or after its own timeout, runs the workflow twice and creates a second Observation, as it would in Mirth. Use a conditional create (ifNoneExist on an identifier built from the device and time) if a repeat must not count twice. Records through one trigger run one at a time, in arrival order. A record that starts with MSH is handled as HL7 and answered with an HL7 ACK.

Reference