vi / websocat

Command-line client for WebSockets, like netcat (or curl) for ws:// with advanced socat-like functions
MIT License
7.17k stars 278 forks source link

How to use websocat with PHP to manipulate values #135

Open rescue1155 opened 3 years ago

rescue1155 commented 3 years ago

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.... ?

vi commented 3 years ago

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"?

rescue1155 commented 3 years ago

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 ?

vi commented 3 years ago

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 commented 3 years ago

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

rescue1155 commented 3 years ago

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).

vi commented 3 years ago

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 commented 3 years ago

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.

vegalou commented 2 years ago

@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.