How to detect when a Printer is added or created, updated, deleted or removed from Javascript
Product JSPrintManager Published 06/26/2020 Updated 11/05/2026 Author Neodynamic
Overview
In this walkthrough, you'll learn how to detect when a new printer is added/created, when a printer is removed/deleted, and when any changes or modifications (updates) are done on any printers available at the client machine.
NOTE: The Printers Watcher is available for Windows clients only.
Try reproducing the following sample code locally...
Step-by-step
-
JSPrintManager Client App
Be sure you install in your dev machine JSPrintManager (JSPM) (Available for Windows, Linux, Raspberry Pi & Mac)
IMPORTANT: This small app must be installed on each client that will use your website!
Since Chrome 142, browsers ask the user to allow Local Network Access the first time your website connects to the JSPrintManager App. -
HTML & Script References
- Download JSPrintManager.js
- Copy all of these *.js files to the same folder where the next
index.htmlpage sample will be located in. - By using your favorite Web Development IDE or Text Editor, create a new HTML file naming it
index.html
Copy/paste the following code inside that html file:
<!DOCTYPE html> <html> <head> <title>JSPrintManager</title> <meta charset="utf-8" /> </head> <body> <div style="text-align:center"> <h1>Printers Watcher from Javascript</h1> <p>Add, remove or change a printer in this Windows machine and see the events below.</p> <hr /> <textarea id="txtPrintersTrace" rows="15" cols="120" readonly></textarea> </div> <!-- JSPrintManager.js (download it or install it with: npm install jsprintmanager) --> <script src="JSPrintManager.js"></script> <script> var printerCreatedEventID = null; var printerUpdatedEventID = null; var printerDeletedEventID = null; // Start the connection with the JSPrintManager Client App JSPM.JSPrintManager.auto_reconnect = true; JSPM.JSPrintManager.start(); JSPM.JSPrintManager.WS.onStatusChanged = function () { if (jspmWSStatus()) { // subscribe to the printers events (Windows clients only) if (!printerCreatedEventID) { printerCreatedEventID = JSPM.JSPrintManager.onPrinterCreated( function (data) { log("PRINTER CREATED! >>> " + JSON.stringify(data)); }, function (err) { console.error(err); }); printerUpdatedEventID = JSPM.JSPrintManager.onPrinterUpdated( function (data) { log("PRINTER UPDATED! >>> " + JSON.stringify(data)); }, function (err) { console.error(err); }); printerDeletedEventID = JSPM.JSPrintManager.onPrinterDeleted( function (data) { log("PRINTER DELETED! >>> " + JSON.stringify(data)); }, function (err) { console.error(err); }); } } }; // Stop watching the printers when leaving the page window.addEventListener('beforeunload', function () { if (printerCreatedEventID) JSPM.JSPrintManager.unsubscribePrinterEvent(printerCreatedEventID); if (printerUpdatedEventID) JSPM.JSPrintManager.unsubscribePrinterEvent(printerUpdatedEventID); if (printerDeletedEventID) JSPM.JSPrintManager.unsubscribePrinterEvent(printerDeletedEventID); }); // Write a line in the printers trace function log(text) { console.info(text); document.getElementById('txtPrintersTrace').value += text + "\r\n"; } // Check the connection status with the JSPrintManager Client App function jspmWSStatus() { var status = JSPM.JSPrintManager.websocket_status; if (status == JSPM.WSStatus.Open) { return true; } else if (status == JSPM.WSStatus.WaitingForUserResponse) { console.info('Waiting for the user to allow this website (JSPrintManager dialog or browser Local Network Access prompt)...'); } else if (status == JSPM.WSStatus.Blocked) { alert('JSPrintManager has blocked this website!'); } else if (status == JSPM.WSStatus.NotInstalled || status == JSPM.WSStatus.Closed) { alert('JSPrintManager Client App is not installed or not running! Download it from https://neodynamic.com/downloads/jspm'); } else if (status == JSPM.WSStatus.CertificateError) { alert('A TLS/SSL certificate error prevented the connection with JSPrintManager Client App.'); } else if (status == JSPM.WSStatus.ConnectionError) { alert('A network error (e.g. a firewall) prevented the connection with JSPrintManager Client App.'); } return false; } </script> </body> </html>
- That's it! Run your website and test it.
Useful? Share it!
| Tweet |
|
Related Articles
How to trace Print Job Status from JavascriptHow to detect virtual & real-physical printers from Javascript
Need help? Contact Support
We provide best-in-class customer service and support directly from members of our dev team! If we are available when you contact us, you will get a response in few minutes; otherwise the maximum turnaround is 24hs in most cases.