websocat icon indicating copy to clipboard operation
websocat copied to clipboard

How to use websocat with PHP to manipulate values

Open rescue1155 opened this issue 2 years ago • 8 comments

Hi There I want to get every time socket values to PHP file and manipulate it and store to my local database.

Is that possible using websocat ?

websocat -t 'wss://stream.SERVER.com:9443/ws/celrusdt@aggTrade' foreachmsg:exec:/home/dev/DATE/b.php

Or any way using linux terminal etc.... ?

rescue1155 avatar Oct 10 '21 07:10 rescue1155

How b.php script is supposed to be used? Does it accept input from stdin? Is there #!...php and executable bit on b.php file?

Maybe you want ... foreachmsg:cmd:"php /home/dev/DATE/b.php"?

vi avatar Oct 10 '21 10:10 vi

websocat -v 'wss://stream.SERVER.com:9443/ws/celrusdt@aggTrade' foreachmsg:cmd:"/usr/bin/php /home/dev/DATA/b.php"

websocat: It is recommended to either set --binary or --text explicitly [INFO websocat::lints] Auto-inserting the line mode [INFO websocat::ws_client_peer] get_ws_client_peer [INFO websocat::ws_client_peer] Connected to ws [INFO websocat::sessionserve] Reverse finished [WARN websocat::foreachmsg_peer] Broken pipe (os error 32) websocat: Broken pipe (os error 32) websocat: error running

I want to get socket every new message to PHP and want to manipulate on PHP and not want to close the socket.


Also I think when once foreachmsg:cmd sent data to file then close the socket connection and re-create new one ??? Am I right ? It it continue sending on every new message receive ?

rescue1155 avatar Oct 10 '21 12:10 rescue1155

Also I think when once foreachmsg:cmd sent data to file then close the socket connection and re-create new one ???

It is not clear what file do you mean. foreachmsg: is designed specifically to instantiate separate connection (or cmd: subprocess) for each message, as oppposed to each connection.

It it continue sending on every new message receive ?

To continue feeding more messages from the same WebSocket connection to the same PHP script instance, you need to omit foreachmsg: and use just cmd:... directly.

vi avatar Oct 10 '21 20:10 vi

Maybe you can also find more luck in using xargs -d '\n' instead of foreachmsg:.

vi avatar Oct 10 '21 20:10 vi

Actually that's not what I am looking for.

I need to listen a web-socket, When disconnect again auto re-connect (Till here that's awesome I like it and works cool on Linux Terminal also)

Now, I need to process the received web socket response to a PHP file and store to own database. So web-socket keep listening and keeping sending me every value in PHP file (On that PHP code I will process logic and store to DB).

rescue1155 avatar Nov 05 '21 13:11 rescue1155

When disconnect again auto re-connect

You can use autoreconnect: overlay for this. If a reconnection happens due to networking problems then some messages may be lost.

vi avatar Nov 05 '21 13:11 vi

process the received web socket response to a PHP file

Why not just connect to WebSocket from inside PHP file? I suppose there are WebSocket libraries for PHP.

If it is problematic to connect to secure wss:// thing, you can use Websocat as a proxy, like this:

websocat -t ws-l:127.0.0.1:1234 autoreconnect:wss://stream.SERVER.com:9443/ws/celrusdt@aggTrade, then connect to ws://127.0.0.1:1234/ from PHP file.

vi avatar Nov 05 '21 13:11 vi

@rescue1155

I think my post could resolve your requirement.

https://github.com/vi/websocat/issues/143

Redirect message from websocket server to PHP, then save into SQL or parse value to applicaiton.

curl -s -X POST http://yourServer/yourPath -d @-

Means POST data via curl from STDIN send by websocat.

Hope this helps.

vegalou avatar Feb 07 '22 05:02 vegalou