If you are using websockets, i have the same problem. I don't think i'm doing something wrong because the script can run for an entire day perfectly, or simply disconnect after a short period. I guess it's disconnected on the server side, not sure we can do anything to avoid it.
i am not.
i might just change API completely.