Last active
September 27, 2024 13:18
-
-
Save Sebastix/5991b84334c6c7e12bc28b2e703311b6 to your computer and use it in GitHub Desktop.
Http/Guzzle async websocket request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $handlerStack = new HandlerStack(new StreamHandler()); | |
| $handlerStack->push(new WebSocketMiddleware()); | |
| $guzzle = new Client(['handler' => $handlerStack]); | |
| // Execute async call | |
| // TODO: research what we can do with this payload array | |
| $payload = []; | |
| /** @var Response $handshakeResponse */ | |
| // https://docs.guzzlephp.org/en/stable/quickstart.html#async-requests | |
| $relay1HandshakeResponse = $guzzle->requestAsync('GET', 'wss://welcome.nostr.wine', $payload)->wait(); | |
| /** @var \Valtzu\WebSocketMiddleware\WebSocketStream $ws */ | |
| $ws = $relay1HandshakeResponse->getBody(); | |
| $reqMsg = populateRequestMessage(); | |
| $py = $reqMsg->generate(); | |
| $ws->write($py); | |
| do { | |
| $response = $ws->read(); | |
| } while ($response === ''); | |
| $result = json_decode($response, false); | |
| $relayResponse = RelayResponse::create($result); | |
| $ws->close(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment