8000 Try to reconnect indefinitely · Issue #5 · dominicletz/websockex · GitHub
[go: up one dir, main page]

Skip to content

Try to reconnect indefinitely #5

Closed

Description

@ericTsiliacos

Does the library support reconnect? When the server goes down, it'd be nice if the websocket client tried to reconnect indefinitely (or have some sort of backoff strategy).

Activity

  1. Azolo commented on May 9, 2017

    @Azolo
    Collaborator

    It's implemented through handle_disconnect. I don't think it will try to connect indefinitely though. I'll look at the overhead of doing that.

    It shouldn't be a problem, but I may suggest to wrap the WebSockex process in a GenServer then monitor the process and then try the reconnecting from there. You would have a lot more control on the backoff strategy that way as well.

  2. changed the title [-]Reconnect[/-] [+]Try to reconnect indefinitely[/+] on May 9, 2017
  3. ericTsiliacos commented on May 9, 2017

    @ericTsiliacos
    Author

    @Azolo thanks for the information! I'm still new to elixir so please forgive me for my ignorance. I have a couple more questions:

    • I'm still not quite sure I understand how returning {:reconnect, state} from handle_disconnect will reconnect. When I do, instead of looping, it raises and exits the process.
    • I noticed that there are quite a few places that have the process exit (from a raise). Given I've wrapped the websocket client in my own custom GenServer for reconnecting, how do I handle the exiting of the process? I see:
        ** (WebSockex.ConnError) Connection Error: :econnrefused
            (websockex) lib/websockex/client.ex:428: WebSockex.Client.handle_disconnect/4
            (websockex) lib/websockex/client.ex:252: WebSockex.Client.init/5
            (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3```
    
  4. Azolo commented on May 9, 2017

    @Azolo
    Collaborator

    @ericTsiliacos I did this off the top off my head and didn't test it at all, but I think this gist should do what you're looking for.

    https://gist.github.com/Azolo/75f6a304b9a835d54c25a8ab543630a0

    To answer your question about returning {:reconnect, state}, what that is doing is trying to reconnect once.

    ** (WebSockex.ConnError) Connection Error: :econnrefused
            (websockex) lib/websockex/client.ex:428: WebSockex.Client.handle_disconnect/4
            (websockex) lib/websockex/client.ex:252: WebSockex.Client.init/5
            (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
    

    Then it's raising a WebSockex.ConnError because you couldn't establish a connection and error'd with :econnrefused. It expects the server to be there when you try to reconnect, when it's not it does a nice 💥.

    An alternative workaround to wrapping the WebSockex process would be to poll the endpoint in the handle_disconnect callback until it was available again then returning {:reconnect, state}.

  5. Azolo commented on May 16, 2017

    @Azolo
    Collaborator

    This was fixed by #7, you'll have to implement another callback but the means are now baked into WebSockex. 😸

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0