tardis-dev / serum-vial

Real-time WebSocket market data API for Serum
Mozilla Public License 2.0
173 stars 60 forks source link

not receive realtime trades forever #17

Closed thanhnguyennguyen closed 3 years ago

thanhnguyennguyen commented 3 years ago

  const subscribeL2 = {
    op: 'subscribe',
    channel: 'trades',
    markets: ['BTC/USDC']
  }

  ws.send(JSON.stringify(subscribeL2))
}```

 i receive some trades right after running code

then no trades update anymore, about 2-3 min, it stop
thaaddeus commented 3 years ago

It's simply because there's no trades in that time, in order to keep connection alive you need to send periodic websocket pings to the server as otherwise it may close idle connections.

thanhnguyennguyen commented 3 years ago

i don't think there's no trades in that time I put all pairs there. Once it stops, i ping again and get a lot of trades, then continue idle

It like a http api, give me trade once we ping to and then nothing until we ping again

Received: all types "recent_trades", no realtime trades

thaaddeus commented 3 years ago

can you please provide standalone code sample that reproduces your issue? do you run serum-vial on your own or use demo server? do you set up your own Solana RPC node or use default one?

thanhnguyennguyen commented 3 years ago

using demo server


const WebSocket = require('ws')
const ws = new WebSocket('wss://serum-vial.tardis.dev/v1/ws')

ws.onmessage = (message) => {
                console.log(message.data)

}
ws.onopen = () => {
  const subscribe = {
    op: 'subscribe',
    channel: 'trades',
    types: 'recent_trades',
    markets: ['RAY/USDT', 'BTC/USDC', 'SRM/USDT', 'SRM/USDC', 'SUSHI/USDC']
  }

ws.send(JSON.stringify(subscribe))
}
thaaddeus commented 3 years ago

I think you need to send periodic pings to the server (https://github.com/websockets/ws#how-to-detect-and-close-broken-connections), on serum for most pairs currently no trade periods can last multiple minutes and then connection is idle and by default would be closed without pings.

thanhnguyennguyen commented 3 years ago

yes, we did it

Look like it always response last 100 trades. It looks like a http api rather than websocket connection with realtime trades

thaaddeus commented 3 years ago

It's not old trades, but recent_trades so after WS connection is established you receive last 100 trades snapshot (type:'recent_trades') and now since connection is open anytime trade happens (and those aren't very frequent most of the time) then you receive trade message.

thanhnguyennguyen commented 3 years ago
const WebSocket = require('ws')
// const ws = new WebSocket('ws://localhost:8000/v1/ws')
const ws = new WebSocket('wss://serum-vial.tardis.dev/v1/ws')

ws.onmessage = (message) => {
    let data = JSON.parse(message.data)
    if (data.type == 'trades' && Array.isArray(data.trades)) {
        data.trades.forEach((value) => {
             console.log("has data", data )
        })
    }
}
ws.onopen = () => {
  const subscribe = {
    op: 'subscribe',
    channel: 'trades',
    types: 'recent_trades',
    markets: ['RAY/USDT', 'BTC/USDC', 'SRM/USDT', 'SRM/USDC', 'SUSHI/USDC']
  }

ws.send(JSON.stringify(subscribe))
}

Filter by type 'trades' for new trades and set this code running in a service to always restart (establish new connection) once the current one is timeout And we don't get any log "has data" in the last 3 days

thaaddeus commented 3 years ago

I've released one improvement, that hopefully should help in our case. Please let me know if now you get the trade data

thaaddeus commented 3 years ago

alternatively please try subscribing via separate ws messages and provide one market in array for each subscription message, I'm looking into the issue that you see - can't reproduce it locally just yet.

thanhnguyennguyen commented 3 years ago

did you update to demo server ?

thaaddeus commented 3 years ago

Hi, I think I found the issue, it was due to underlying WebSocket library having issues with SSL, I turned it off for origin demo server and SSL/TLS is now handled by Cloudflare only, but that required different domain (as couldn't pull it off on existing one). Here's new url for demo server wss://api.serum-vial.dev/v1/ws - I've tested it and seems to behave stable even when subscribing to 40 symbols at once.

thanhnguyennguyen commented 3 years ago

yes. it works now Thank you so much

One additional question: how can we see proof of trade on Solana explorer ? (Any txhash, signature relating to the trade) I check trades from log and look at on explorer https://solanabeach.io/ , doesn't match any trade amount , so weird

thaaddeus commented 3 years ago

Great, thanks for confirming. I was looking into tx info to match, but as far as I know it's not possible to know that, at least I wasn't able to get it from RPC node.

thanhnguyennguyen commented 3 years ago

so nothing to prove that the trade happen

We receive tradeId, how can we use that trade ID to track the trade ?

thaaddeus commented 3 years ago

Trade id can't be used as it's generated based on match order data (order id, size, timestamp etc), but you can use trade slot field and look for orders that happened for given slot/block on https://solanabeach.io/address/9wFFyRfZBsuAha4YcuxcXLKwMxJR43S7fPfQLusDBzvT/recent-orders for example but since each block can have thousands of transactions it's not that straightforward to find, but possible. There's no other way as far as I know.

thanhnguyennguyen commented 3 years ago

we may look into to see if we can get signature (txhash) of trade transaction