@frostbyte-os/react-native-escpos
v1.0.3
Published
react native network adapter for escpos
Readme
@frostbyte-os/react-native-escpos
React Native network adapter for ESC/POS receipt printers. Opens a raw TCP socket to a network printer and writes ESC/POS byte sequences to it.
Node's net module does not exist in React Native, so this wraps
react-native-tcp-socket behind the
same adapter shape the escpos ecosystem expects.
Install
npm install @frostbyte-os/react-native-escpos react-native-tcp-socketreact-native-tcp-socket is a peer dependency, so you install it yourself. Follow its own
setup steps for the native side, including the Android INTERNET permission and a pod install
for iOS.
Usage
import Network from "@frostbyte-os/react-native-escpos";
const printer = new Network("192.168.1.50", 9100);
printer.open((err) => {
if (err) {
console.warn("printer unreachable", err);
return;
}
// ESC @ = initialize, then text, then GS V 0 = full cut
printer.write(Buffer.from([0x1b, 0x40]));
printer.write(Buffer.from("Hello world\n", "ascii"));
printer.write(Buffer.from([0x1d, 0x56, 0x00]), () => {
printer.close();
});
});Most network receipt printers listen on TCP 9100, which is the default when you omit the port.
API
new Network(address, [port])
Creates an adapter. port defaults to 9100. Does not connect; call open() for that.
.open(callback, [params])
Opens the socket. callback(err, device) fires on connect, and again with an error if the socket
errors. params.onSocketKill(device) is invoked when the socket errors out.
.write(data, [callback])
Writes bytes to the printer. data is a Buffer, Uint8Array, or string. callback fires once
the bytes are flushed.
.read(callback)
Subscribes to inbound data, callback(buffer) per chunk. Useful for real-time status back from
printers that support it.
.close([callback])
Destroys the socket and emits disconnect. callback(null, device) fires afterward.
Events
Network extends EventEmitter and emits disconnect when close() runs.
Known issues
isConnected is defined both as an instance boolean and as a prototype method, and the boolean
wins. Read it as a property (printer.isConnected) rather than calling it, since
printer.isConnected() throws.
License
MIT
