AllYourBot / hostedgpt

An open version of ChatGPT you can host anywhere or run locally.
MIT License
269 stars 111 forks source link

Improve the automatic reconnect behavior (for actioncable) #440

Open krschacht opened 1 week ago

krschacht commented 1 week ago

See here: https://github.com/hotwired/turbo/issues/1261#issuecomment-2125650736

This is the old code I was using:

import { cable } from "@hotwired/turbo-rails"

window.isConnected = true
window.cable = cable
window.consumer = await cable.createConsumer()
setInterval(() => {
  if (consumer.connection.isOpen() != window.isConnected) {
    window.isConnected = consumer.connection.isOpen()
    console.log(`cable ${window.isConnected ? 'connected' : 'DISCONNECTED'}`)
    const elem = document.getElementById('connection-status')
    if (!window.isConnected) {
      if (elem) {
        elem.classList.add('bg-red-400')
        elem.classList.add('text-white')
      }
    } else {
      if (elem) {
        elem.classList.remove('bg-red-400')
        elem.classList.remove('text-white')
        elem.classList.add('text-gray-200')
      }
    }
  }
}, 500)
window.consumer.connection.open()