19

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

Irina
  • 939
  • 1
  • 8
  • 26

2 Answers2

8

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.

grddev
  • 2,671
  • 20
  • 22
Dheeraj Thedijje
  • 1,053
  • 12
  • 19
  • 5
    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. – adamczi May 11 '20 at 21:35
  • 1
    @adamczi no, you can get a reference to the websocket instance the page is already using and use that. – odinho - Velmont Jun 24 '20 at 12:09
  • 16
    @adamczi 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. – odinho - Velmont Jun 24 '20 at 13:05
8
  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({websocket: 'message'}).

Chrome console with found WebSockets

Mišo
  • 2,327
  • 1
  • 23
  • 25