In Firefox/Chrome devtools Is there a way to send/edit websocket messages after connection
Asked Answered
D

2

29

I have a Safari/Firefox/Chrome browser. My browser has devtools. Is there a way to happy send/edit websocket messages for existing connection? Or by plugin?

Thank you

Disassemble answered 9/12, 2019 at 3:43 Comment(0)
A
7

You can grab instance of websocket connection and can use it further to send further messages on it.

Grab socket connection instance

You must be aware of websocket connection establishment as below:

websocket = new WebSocket('your-ws-url-goes-here');

Now you can use instance of websocket and can use .send() and .close().

Your question states that you want to use existing connected web socket, you can look for socket connection instance in source code and can use it for sending further messages.

Example to play with

You can play with websocket and its instance here at http://websocket.org/echo.html

Notice here

var wsUri = "wss://echo.websocket.org/";

and function having

websocket = new WebSocket(wsUri);

You so you know websocket is connected and having instance in websocket

You can open devtool and type websocket to see all of the option. So in your case you need to find instance of connection so you can play with it.

About editing existing message

I couldn't find if there is any way to edit sent messages, and i think it should not be there. You can send new message since earlier message must have been responded already.

Arad answered 9/12, 2019 at 5:56 Comment(3)
This method only allows to create another WebSocket connection to the server, it doesn't allow to reuse existing connection. OP asked for sending messages within existing connection.Straighten
@Straighten no, you can get a reference to the websocket instance the page is already using and use that.Pessary
@Straighten Getting websocket instance: devtools -> memory -> heap dump, search "websocket" and expand, right click some of them -> store as a global variable. There might be several so you need the correct one, check temp1.url to see.Pessary
P
19
  1. You can list all WebSocket connections on page in Chrome by opening a console and writing queryObjects(WebSocket). It should list all instances after a while.
  2. Then choose the one you want to use and right-click on it and choose "Store object as global variable".
  3. This will create a new variable like temp1 so you can send messages with temp1.send(JSON.stringify({websocket: 'message'})).

Chrome console with found WebSockets

Penny answered 16/2, 2023 at 7:52 Comment(2)
It's temp1.send('{your: "message"}')Untouchable
This doesn't work on Firefox, only chromium based browserrsEward
A
7

You can grab instance of websocket connection and can use it further to send further messages on it.

Grab socket connection instance

You must be aware of websocket connection establishment as below:

websocket = new WebSocket('your-ws-url-goes-here');

Now you can use instance of websocket and can use .send() and .close().

Your question states that you want to use existing connected web socket, you can look for socket connection instance in source code and can use it for sending further messages.

Example to play with

You can play with websocket and its instance here at http://websocket.org/echo.html

Notice here

var wsUri = "wss://echo.websocket.org/";

and function having

websocket = new WebSocket(wsUri);

You so you know websocket is connected and having instance in websocket

You can open devtool and type websocket to see all of the option. So in your case you need to find instance of connection so you can play with it.

About editing existing message

I couldn't find if there is any way to edit sent messages, and i think it should not be there. You can send new message since earlier message must have been responded already.

Arad answered 9/12, 2019 at 5:56 Comment(3)
This method only allows to create another WebSocket connection to the server, it doesn't allow to reuse existing connection. OP asked for sending messages within existing connection.Straighten
@Straighten no, you can get a reference to the websocket instance the page is already using and use that.Pessary
@Straighten Getting websocket instance: devtools -> memory -> heap dump, search "websocket" and expand, right click some of them -> store as a global variable. There might be several so you need the correct one, check temp1.url to see.Pessary

© 2022 - 2024 — McMap. All rights reserved.