No valid WebSocket class provided
caiqueacct opened this issue · 2 comments
Hi
I'm trying to use coinbase-pro-node in a Vue app but I'm receiving the following error when trying to use the websocketclient
Error: No valid WebSocket class provided
_connect reconnecting-websocket-mjs.js:506
ReconnectingWebSocket reconnecting-websocket-mjs.js:203
connect WebSocketClient.js:159
_callee$ VCoinbaseSocket.vue:49
Babel 10
VueJS 4
click vuetify.js:2564
VueJS 33
VCoinbaseSocket.vue:51
Any ideas?
Thanks
The "coinbase-pro-node" library was developed to run in a Node.js environment. However, it is possible to use the package in a browser environment (where Vue.js frontend apps are running) but you have to be aware of some consequences:
- REST calls are likely to be blocked by the browser's CORS policy, so you have to start your browser with disabled web security or use a proxy (i.e. webpack's dev server proxy) to communicate with Coinbase Pro's backend.
- WebSocket connections are established using the "ws" package (Node.js), but the browser comes with a native WebSocket implementation, so the constructor passed to
ReconnectingWebSocket
must be changed (Example).
Are you just having problems with establishing a WebSocket connection or do REST calls also fail for you?
Thank you for the update. I was able to get this working using the link that you sent.
async coinbaseRealtime() {
this.ws = new ReconnectingWebSocket(
'wss://ws-feed.pro.coinbase.com',
[],
{
key: '',
passphrase: '',
signature: '',
timestamp: Date.now() / 1000
}
)
this.ws.addEventListener('error', (e) => {
console.debug('error', e)
})
this.ws.addEventListener('open', () => {
console.debug('Connection open')
const subscribe = {
"type": 'subscribe',
"channels": [
{
"name": 'ticker',
"product_ids": ['BTC-USD', 'ETH-USD', 'LTC-USD']
}
]
}
this.ws.send(JSON.stringify(subscribe))
})
this.ws.addEventListener('message', (data) => {
console.debug('Received message', data)
})
}