Websocket in Advanced Trading is a hot mess. Here's a PHP example for those who need it

TLDR; Here is a little bit of PHP using the TexTalk library for those who don’t want to rip their hair out.

I struggled for the better part of 2 days trying to figure out wtf was going on after following the api documents to the letter. It wasn’t until I printed the frigging JS example out that I realized they’d converted the timestamp to a string, where the api docs just above showed it passed as an integer.
Much like everything else coinbase, the error is quite informative by saying nothing about timestamp being invalid..

After that, I realized that even though the v2 API will give me every symbol back, I can ONLY push the symbols my api key had access to, or it would would accept the json subscribe and silently sit there until the socket times out.

Anyway… Don’t be dumb. Plagiarize

        while (true) {
            echo("Starting CB Websocket\n");
            $timestamp = str(time());
            $channel = 'ticker';
            $symbols = ['BTC-USD', 'ETH-USD'];
            $symbol_list = implode(',', $symbols);
            $signature_string = "$timestamp$channel$symbol_list";
            $signature = hash_hmac('sha256', utf8_encode($signature_string), utf8_encode('YOUR SECRET'), false);
            $client = new Client("wss://advanced-trade-ws.coinbase.com:443");
            $subscribe = [
                'type'          => 'subscribe',
                'channel'       => $channel,
                'api_key'       => 'YOUR API',
                'product_ids'   => $symbol_array,
                'signature'     => $signature,
                'timestamp'     => $timestamp,
            ];
            $json = json_encode($subscribe);
            $client->text($json);
            while($client){
                try {
                    $message =  $client->receive();
                    if ($message) {
                        $json_message = json_decode($message, true);
                        # We can have events without an event... Makes total sense.
                        if (!isset($json_message['events'])) { 
                            continue;
                        }
                        foreach ($json_message['events'] as $event) {
                            # Because why the **** not... this doesn't have to be here for some reason
                            if (!isset($event['tickers'])) { 
                                continue;
                            }
                            foreach ($event['tickers'] as $ticker) {
                                $symbol = str_replace("-", "/", $ticker['product_id']);
                                $price = $ticker['price'];
                                # Did you know that sometimes a ticker can return with all the information, but a null price? Yup...
                                if (! $price) {
                                    continue;
                                }
                                ## WHATEVER YOU WANT TO DO WITH THE PRICE AND SYMBOL ##
                            }
                        }
                    }
                } catch (\WebSocket\ConnectionException $e) {
                    echo("Websocket disconnected. Restarting\n");
                    break;
                }
            }
            sleep(5);
        }
1 Like

Being that this isn’t that far off from today, i’m going to chime in and say the example js shows the string conversion, but yeah. I made the string change on my end and all i’m getting is “null” return now.

Hi @hawtdogflvrwtr! Welcome to the Coinbase Cloud Developer’s Forum!

We appreciate your interest in Coinbase Cloud products. We would also like to thank you for your effort in bringing this up to our attention and sharing this information with the community. Please note that we have tagged your concern about updating the sample subscribe message in the documentation, specific to the value type that was intended for the timestamp to be used in the subscribe message of Advanced Trade Websocket, as a feedback for now and have our internal teams look into this.

Most new features and improvements to our products come directly from feedback like yours, so it’s very valuable to us. While we can’t offer any specific timeline for adding features and improvements, we are constantly working to build products our customers will love. If you want to stay up to date on the latest from Coinbase Cloud, you can also bookmark the following webpage and subscribe to email updates at the bottom of the page: https://www.coinbase.com/cloud/discover

Thank you and have a great day!

1 Like