react-native-esc-pos-printer icon indicating copy to clipboard operation
react-native-esc-pos-printer copied to clipboard

Instantiate printer does not work on multiple devices

Open ahmad71666 opened this issue 1 year ago • 14 comments

If i try to use printer on two devices using instantiate method it does not work, it works with init method, I work instantiate printer on one device, print and then diconnectprinter using target, same for device two but sometimes the printer does not work. I'll have to wait for 40 seconds before using printer on other there's some issue with the printer disconnection function.

ahmad71666 avatar Nov 21 '23 09:11 ahmad71666

If you have only one printer please use init instead - it is more stable.

If you have more then one printer - then you should use instantiate, but ti still must be reworked.

tr3v3r avatar Nov 21 '23 12:11 tr3v3r

@tr3v3r I have 4 printers, previously using init method but it takes around 1 minute and few seconds to print on all devices, instantiate method prints in almost 10 seconds, but the problem is with two tablets are connected with the printer, If i close application on one tablet then the second one works, if i close on the second then it works on the first one, I am even calling disconnect function, what I think it's caching the printers or not deallocate properly.. is this something which can be fixed? that would be great

ahmad71666 avatar Nov 21 '23 12:11 ahmad71666

I see

I'd like to help but unfortunately, I have only one printer. So I can't reproduce such cases and test. I have a plan to re-implement implementation soon.

For now, the only thing I can advise is to disconnect the printer when the application goes to a background state.

tr3v3r avatar Nov 21 '23 14:11 tr3v3r

You can test it by single printer as well, just use instantiate method, save printer object on two devices and print one by one, each time instantiate printer and print and then disconnect, it supposed to disconnect the printer but it does not. hope you understand @tr3v3r

ahmad71666 avatar Nov 21 '23 14:11 ahmad71666

@ahmad71666 ok I see. could you please describe step by step what to do to reproduce it?

i.e.

  1. connect to Printer from device A
  2. connect to Printer from device B

...

Then give expected result and actual result. Also please give details about phones (android , ios or doesn't matter )

It would really help to resolve it faster

Also, it would be great if you could share the piece of code how you print.

tr3v3r avatar Nov 21 '23 14:11 tr3v3r

@ahmad71666 I did a small POC and added the possibility to print on multiple printers for init method.

Can I ask you to test this scenario please with these changes?

To test it do please the following steps:

  1. install package from this branch instead of npm
  2. Test on iOS ( then I will update Android if it works )
  3. Test using init method
  4. At the end of the printing chain add printerId property. Example

    await EscPosPrinter.init({
        target: printer.target,
        seriesName: getPrinterSeriesByName(printer.deviceName),
        language: 'EPOS2_LANG_EN',
      });

  const printing = new EscPosPrinter.printing();

  await printing
    .initialize()
    .align('center')
    .size(3, 3)
    .line('DUDE!')
    .cut()
    .send({ printerId: printer.target }); // <-- put printer target here
  1. do not use any API except printing ( no monitoring or getting paper width )

tr3v3r avatar Nov 22 '23 22:11 tr3v3r

  1. this branch

Hi, I'll write down the whole scenario, I have two devices Samsung A8 Tab, and Oneplus 8, Both are android and devices are not the issue here. Now I am using Printer Epson Tm-m30II for printing which is connected to both devices, I discover on both devices and save the object which contains target etc,

Now when I start printing, I get the printer object instantiate it, and then give print command, and then after that I call the disconnect function, to disconnect the printer from the first device.

Then I do these steps on the second device, the printer does not work on the second device, but when I close my application on the first device and send print command from the second device, it works and vice versa.

That means the disconnection is not working properly and somehow the printer remain connected with the first device even if I call the disconnect function multiple times it still remain connected until unless i turn off application on first device and then give print command from the second one... I hope it clears what's happening, right now I am facing this issue, and it's happening if there's one or more printers the issue remains the same.

Now I'll share code.. I don't want to use init method because at the same time it does not get connected to multiple printers and it takes alot more time then the instantiate method.

// connection

await connectPrinters(printers)

import EscPosPrinter, { getPrinterSeriesByName } from 'react-native-esc-pos-printer';

const printerConnect = async (printer) => {

try {
    await EscPosPrinter.instantiate({
        target: printer.target,
        seriesName: getPrinterSeriesByName(printer.name),
        language: 'EPOS2_LANG_EN',
    });
    return true;
} catch (error) {
    console.log(error, "Error Connecting Printer");
    return false;
}

}

export const connectPrinters = async (printers) => {

const toBeConnected = printers?.map(printer => printerConnect(printer))

const result = await Promise.all(toBeConnected)

console.log(result,"result");

}

// Here printing the receipts

import EscPosPrinter, { getPrinterSeriesByName } from 'react-native-esc-pos-printer';

export const receiptSmall = async ({ order, printerSetup }) => {

let errorPrinting = false;

try {

const dateTime = order?.receipt_dateTime

const printer = printerSetup

const printing = new EscPosPrinter.printing();

await printing
  .initialize()
  .newline()
  .newline()
  .align('center')
  .size(3, 3)
  .text(order?.callNo?.toString())
  .newline()
  .cut()
  .send({
    target: printer.target,
  });

} catch (error) { console.log('Error while printing:', error); errorPrinting = true; } return errorPrinting; }

// Here printing the receipts

disconnection

await disconnectPrinters(printers)

import EscPosPrinter from 'react-native-esc-pos-printer';

const printerDisconnect = async (target) => {

try {
    await EscPosPrinter.disconnectPrinter(target)
    return true;
} catch (error) {
    console.log(error, "Error Disconnecting Printer");
    return false;
}

}

export const disconnectPrinters = async (printers) => {

const toBeDisconnected = printers?.map(printer => printerDisconnect(printer?.target))

const result = await Promise.all(toBeDisconnected)

console.log(result,"result");

}

ahmad71666 avatar Nov 23 '23 22:11 ahmad71666

@tr3v3r please check the above, Thanks alot

ahmad71666 avatar Nov 23 '23 22:11 ahmad71666

@ahmad71666 have you checked what I've posted? I said that I made init method work with multiple printers (like instantiate does)

tr3v3r avatar Nov 24 '23 08:11 tr3v3r

@tr3v3r I'm having a similar issue to this. printing to multiple printers works fine on IOS but Android seems to hold on to the connection after the first instantiation and then i cant fire off another print for around 40 seconds, i just get a time out error in the instantiate function.

bgreendesign avatar Dec 21 '23 16:12 bgreendesign

@tr3v3r I have tried to apply the changes from your branch, and it seems to have resolved this issue we experiencing

vsnaichuk avatar Feb 15 '24 15:02 vsnaichuk

@tr3v3r it works, please update for android as well.

manage-some avatar Feb 24 '24 14:02 manage-some

@ahmad71666 did you get any solution ?

OhFarhan avatar Feb 25 '24 07:02 OhFarhan

Just raised a PR and need your help guys to test it. Should solve your issues

https://github.com/tr3v3r/react-native-esc-pos-printer/pull/139

tr3v3r avatar Mar 13 '24 21:03 tr3v3r

Please check the 4.0.0 version. It has completely reworked way of printing

tr3v3r avatar Jul 25 '24 09:07 tr3v3r