hl7v2_mllp 0.1.1
hl7v2_mllp: ^0.1.1 copied to clipboard
MLLP (Minimum Lower Layer Protocol) transport for HL7v2 messages. TCP client and server with TLS support and automatic reconnection.
// ignore_for_file: avoid_print, unused_local_variable
import 'package:hl7v2_parser/hl7v2_parser.dart';
import 'package:hl7v2_mllp/hl7v2_mllp.dart';
/// Example showing MLLP client and server usage.
///
/// In a real application, client and server would run in separate processes.
/// This example shows both in one file for illustration.
void main() async {
// ── Server ───────────────────────────────────────────────────────────
final server = MllpServer(port: 0); // port 0 = OS-assigned
await server.start();
print('Server listening on port ${server.boundPort}');
// Handle incoming messages
server.messages.listen((envelope) async {
final message = Message.parse(envelope.payload);
print('Server received: ${message.messageType}');
// Send ACK back
final ack = message.generateAck(code: AckCode.aa);
await envelope.respond(ack.encode());
});
// ── Client ───────────────────────────────────────────────────────────
final client = MllpClient(
host: 'localhost',
port: server.boundPort,
);
await client.connect();
// Build and send a message
final order = MessageBuilder()
.msh((b) => b
.sendingApplication('APP')
.sendingFacility('HOSP')
.receivingApplication('LIS')
.receivingFacility('LAB')
.messageType('ORM', 'O01')
.version('2.5'))
.addSegment('PID', (b) => b
.field(3, '12345^^^HOSP^MR')
.field(5, 'DOE^JOHN'))
.build();
final response = await client.send(order.encode());
final ack = Message.parse(response);
print('Client received ACK: ${ack.get('MSA-1')}'); // AA
// ── Cleanup ──────────────────────────────────────────────────────────
await client.close();
await server.stop();
print('Done.');
// ── TLS example (commented — requires certificates) ──────────────────
//
// final tlsConfig = MllpTlsConfig.fromPem(
// certChainPath: 'certs/client.pem',
// privateKeyPath: 'certs/client.key',
// );
//
// final tlsClient = MllpClient(
// host: 'hl7.hospital.org',
// port: 2576,
// tlsConfig: tlsConfig,
// );
// ── Reconnection example (commented) ─────────────────────────────────
//
// final reconnectClient = MllpClient(
// host: '192.168.1.100',
// port: 2575,
// reconnectConfig: ReconnectConfig(
// initialDelay: Duration(seconds: 1),
// maxDelay: Duration(seconds: 30),
// maxAttempts: 10,
// ),
// );
//
// reconnectClient.reconnectEvents.listen((event) {
// print('Reconnect: ${event.type.name}');
// });
}