question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

proper auto-reconnection from sockets

See original GitHub issue

Hi Jan,

Here’s how we do the connection / reconnection to sockets in F#:

        let rec subscribeToTrades () =
            let r =
                socketClient.SubscribeToAggregatedTradeUpdatesAsync(symbol, fun trade -> tradesProcessor.Post(trade))
                |> Async.AwaitTask
                |> Async.RunSynchronously
            if not r.Success then failwithf "couldn't subscribe to trades updates -  %i:%s" r.Error.Code r.Error.Message
            else
                r.Data.add_ConnectionLost     (fun _ -> eprintfn "%A - trades socket connection lost" DateTime.UtcNow)
                r.Data.add_ConnectionRestored (fun _ -> eprintfn "%A - trades socket connection restored" DateTime.UtcNow
                                                        subscribeToTrades())

as you wrote the lib, could you confirm if this is optimal, or if there would be something you’d do differently when it comes to the subscription / reconnection mechanism?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
JKorfcommented, Sep 15, 2020

No, listenkey should indeed be handled manually. The advise is to call KeepAlive every 30 minutes, but I don’t think there is any downside to calling it every 10 minutes. I think you only get the listen key expired after 60 minutes without keep alive, so you shouldn’t run into that.

Looks just fine the way you’re handling it.

0reactions
thomasd3commented, Sep 15, 2020

actually a related question: do you handle the listen key directly too, for the SubscribeToUserDataUpdatesAsync call?

right now I resubscribe when if the license key is expired.

I do something like this:

       // get the user data updates
        let rec subscribeToUserDataUpdates () =
            // get the user data
            listenKey <-
                (
                    restClient.StartUserStreamAsync()
                    |> Async.AwaitTask
                    |> Async.RunSynchronously
                ).Data

            let r =
                socketClient.SubscribeToUserDataUpdatesAsync
                    (
                        listenKey,
                        (fun _ -> ()),                                          // cross wallet update
                        (fun _ -> ()),                                          // margin update
                        (fun balances  -> balancesProcessor.Post(balances)),    // balances
                        (fun positions -> positionsProcessor.Post(positions)),  // positions
                        (fun order     -> ordersProcessor.Post(order)),         // orders
                        (fun _         -> subscribeToUserDataUpdates())         // listenKey expired
                    )
                    |> Async.AwaitTask
                    |> Async.RunSynchronously
            if not r.Success then failwithf "couldn't subscribe to user data updates -  %i:%s" r.Error.Code r.Error.Message
            else
                r.Data.add_ConnectionLost     (fun _ -> eprintfn "%A - user data socket connection lost" DateTime.UtcNow)
                r.Data.add_ConnectionRestored (fun _ -> eprintfn "%A - user data socket connection restored" DateTime.UtcNow
                                                        subscribeToUserDataUpdates())

I also have this that goes with it:

        // set the listenkey timer
        let updateTimerInterval = TimeSpan.FromMinutes(10.)
        listenKeyTimer.Elapsed.Add (fun _ ->
            restClient.KeepAliveUserStreamAsync(listenKey)
            |> Async.AwaitTask
            |> Async.RunSynchronously
            |> ignore)
        listenKeyTimer.Interval <- updateTimerInterval.TotalMilliseconds
        listenKeyTimer.Enabled  <- true
        listenKeyTimer.Start()
Read more comments on GitHub >

github_iconTop Results From Across the Web

The right way for a client to reconnect after the server does ...
Server: node + socket.io var io = require( 'socket.io' )( 3000 ); io.on( ... I upgraded to version 2.0.3 and its default autoreconnect...
Read more >
javascript - Socket IO reconnect?
The right way to fix it is: var io = null; DB.connect(function () { io = require('socket.io').listen(8080); io.sockets.on('connection', ...
Read more >
TIP: Auto Reconnect Web Socket
With the latest update to the WebSocket cn1lib we now have a better solution: autoReconnect(int) . It's exactly as it sounds, once you...
Read more >
The Socket instance (client-side)
The Socket instance emits three special events: connect; connect_error; disconnect. Please note that since Socket.IO v3, the Socket instance ...
Read more >
Scaling Socket.IO - practical considerations
Regardless of how good your hardware is, you can only scale it vertically up to a finite capacity. What happens if, at some...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found