1
0
Fork 0
mirror of synced 2024-09-18 18:28:33 +12:00
budibase/packages/client/src/websocket.js

41 lines
1.2 KiB
JavaScript
Raw Normal View History

import {
builderStore,
environmentStore,
notificationStore,
} from "./stores/index.js"
2022-08-19 22:09:20 +12:00
import { get } from "svelte/store"
import { io } from "socket.io-client"
export const initWebsocket = () => {
const { inBuilder, location } = get(builderStore)
const { cloud } = get(environmentStore)
2022-08-23 05:24:34 +12:00
// Only connect when we're inside the builder preview, for now
if (!inBuilder || !location || cloud) {
2022-08-19 22:09:20 +12:00
return
}
2022-08-23 05:24:34 +12:00
// Initialise connection
2022-08-19 22:09:20 +12:00
const tls = location.protocol === "https:"
const proto = tls ? "wss:" : "ws:"
const host = location.hostname
2022-08-23 19:48:16 +12:00
const port = location.port || (tls ? 443 : 80)
2022-08-19 22:09:20 +12:00
const socket = io(`${proto}//${host}:${port}`, {
2022-08-23 05:24:34 +12:00
path: "/socket/client",
// Cap reconnection attempts to 10 (total of 95 seconds before giving up)
reconnectionAttempts: 10,
// Delay initial reconnection attempt by 5 seconds
reconnectionDelay: 5000,
// Then decrease to 10 second intervals
reconnectionDelayMax: 10000,
// Timeout after 5 seconds so we never stack requests
timeout: 5000,
2022-08-19 22:09:20 +12:00
})
2022-08-23 05:24:34 +12:00
// Event handlers
2022-08-19 22:09:20 +12:00
socket.on("plugin-update", data => {
builderStore.actions.updateUsedPlugin(data.name, data.hash)
notificationStore.actions.info(`"${data.name}" plugin reloaded`)
2022-08-19 22:09:20 +12:00
})
}