1

On my HTML page, I am trying to perform web-automation on a websocket with puppeteer. I've tried different ways of running this code, but every time I try, I get a variety of different errors. I was wondering if there is a specific code I could use in order to make this work?

My HTML code:

<script src="https://unpkg.com/puppeteer-web">
</script>
<script>
async function run() {
let socket = new WebSocket("wss://javascript.info/article/websocket/demo/hello");

socket.onopen = function(e) {
  console.log("[open] Connection established");
  console.log("Sending to server");
  socket.send("hey");
};

socket.onmessage = function(event) {
  console.log(`[message] Data received from server: ${event.data}`);
};

socket.onerror = function(error) {
  console.log(`[error] ${error.message}`);
};

const browser = await puppeteer.connect({socket});
    const pagesCount = (await browser.pages()).length;
}
run();

</script>
coder420
  • 163
  • 3
  • 8

1 Answers1

1

Earlier it was possible with puppeteer-web, but not anymore. Unpackaged versions of puppeteer won't work anymore even if you've added them in your HTML source.

You need to use puppeteer on the server (backend) side. So if your app is client side-only so far: you will need a Node.Js server as well to run the scrapers and to make their output data available to your frontend app via an endpoint (most probably you would choose Express for this purpose).

theDavidBarton
  • 7,643
  • 4
  • 24
  • 51