Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing Websocket Messages From Network Tab

Using puppeteer I am trying to read websocket messages and put them into a string to output onto the console. To see an example of some websocket messages you can visit the link provided below within the code and then follow the steps within the parenthesis. (CTRL+SHIFT+I > Network > WS (websocket) > Messages)

var puppeteer = require('puppeteer');

async function run() {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();

    await page.goto('http://powerline.io');
    console.log('navigated successfully');
    page.on('response', response => {
        const isWSS = ['websocket'].includes(response.request().resourceType())

        if (isWSS){
            console.log(isWSS);
            log(response.url());
            response.text().then(log)
        }
    })
}

run();

1 Answers

Here is an example, how it can be captured:

var puppeteer = require('puppeteer');

async function run() {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  await page.goto('http://powerline.io');
  const cdp = await page.target().createCDPSession();
  await cdp.send('Network.enable');
  await cdp.send('Page.enable');

  const printResponse = response => console.log('response: ', response);

  cdp.on('Network.webSocketFrameReceived', printResponse); // Fired when WebSocket message is received.
  cdp.on('Network.webSocketFrameSent', printResponse); // Fired when WebSocket message is sent.
}

run();

Read more about network events.

like image 180
Yevhen Laichenkov Avatar answered Sep 04 '26 17:09

Yevhen Laichenkov