TCP Printing with React Native Expo: Subsequent Print Jobs Only Processed After App Restart

I’m working on a React Native application that sends print jobs to an Intermec PC43d printer using TCP and ZPL commands. The issue I’m encountering is that the first print job is processed correctly, but any subsequent print jobs are only processed when I restart the application.

Here’s what happens:
First Print Job: The label prints correctly.
Subsequent Print Jobs: No labels are printed until the application is closed and restarted. Upon restart, the queued print jobs are processed.

Current Implementation:

import React, { useState } from 'react';
import { SafeAreaView, useColorScheme, Button, TextInput, Alert } from 'react-native';
import { Colors } from 'react-native/Libraries/NewAppScreen';
import TcpSocket from 'react-native-tcp-socket';

const PrintTestScreen = () => {
  const isDarkMode = useColorScheme() === 'dark';

  const [state, setState] = useState({
    text: `
      ^XA
      ^FX Top section with logo, name and address.
      ^CF0,60
      ^FO50,50^GB100,100,100^FS
      ^FO75,75^FR^GB100,100,100^FS
      ^FO93,93^GB40,40,40^FS
      ^FO220,50^FDTesting.^FS
      ^XZ
    `,
  });

  const backgroundStyle = {
    backgroundColor: isDarkMode ? Colors.darker : Colors.lighter,
  };

  const onPress = async () => {
    const ip = '192.168.1.114';
    const port = 9100;

    console.log(`Attempting to create connection to ${ip}:${port}`);

    let client;
    try {
      client = TcpSocket.createConnection({ port, host: ip }, () => {
        console.log('Successfully created connection');

        const zplCommand = state.text.trim();
        const printCommand = zplCommand;

        console.log(`Sending ZPL command: ${printCommand}`);
        client.write(printCommand, (writeErr) => {
          if (writeErr) {
            console.error(`Error sending data: ${writeErr.message}`);
            cleanup(client);
            return;
          }

          console.log(`Sent print job to ${ip}:${port} at ${new Date().toISOString()}`);

          // Ensure the command is fully sent before ending and destroying the connection
          setTimeout(() => {
            cleanup(client);
          }, 3000); // Increased delay to ensure the command is fully processed
        });

        client.on('error', (error) => {
          console.error(`Printing failed due to: ${error.message}`);
          cleanup(client);
        });

        client.on('close', () => {
          console.log('Connection closed');
        });
      });

    } catch (error) {
      console.error(`Error in sendToPrinter: ${error.message}`);
      Alert.alert('Printing Error', error.message);
      if (client) {
        cleanup(client);
      }
    }
  };

  const cleanup = (client) => {
    if (client) {
      client.end(() => {
        console.log('Connection ended');
        client.destroy();
        console.log('Connection explicitly destroyed after printing');
      });
    }
  };

  return (
    <SafeAreaView style={backgroundStyle}>
      <TextInput
        value={state.text}
        onChangeText={(text) => setState((prev) => ({ ...prev, text }))}
        style={{ padding: 10, borderWidth: 1, margin: 10 }}
      />
      <Button
        title="Click here to Print label!"
        color="#841584"
        onPress={onPress}
      />
    </SafeAreaView>
  );
};

export default PrintTestScreen;

Here is the output from my console log. It appears that the connection is never explicitly destroyed, but I don’t believe this is the root cause of the issue.

LOG Attempting to create connection to 192.168.1.114:9100
LOG Successfully created connection
LOG Sending ZPL command: ^XA
^FX Top section with logo, name and address.
^CF0,60
^FO50,50^GB100,100,100^FS
^FO75,75^FR^GB100,100,100^FS
^FO93,93^GB40,40,40^FS
^FO220,50^FDTesting.^FS
^XZ

Any insights or suggestions on how to ensure the subsequent print jobs are processed without restarting the application would be greatly appreciated!

I implemented TCP printing in a React Native application using the react-native-tcp-socket library to send ZPL commands to an Intermec PC43d printer. The expected behavior was that each print job would be processed immediately upon sending the command.

Here is a summary of what I tried:

Connection Management: I ensured the TCP connection was properly closed after each print job by using client.end and client.destroy.
Delays: I added delays to ensure the commands were fully processed before closing the connection.

What actually happened?
The first print job prints correctly, but subsequent print jobs are only processed when the application is restarted. This means that while the commands are sent and acknowledged, the printer does not print them until the app is restarted.

What did you expect to happen?
I expected each print job to be processed immediately without the need to restart the application.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật