budibase/packages/client/src/websocket.js

27 lines
733 B
JavaScript
Raw Normal View History

2022-08-19 12:09:20 +02:00
import { builderStore } from "./stores/index.js"
import { get } from "svelte/store"
import { io } from "socket.io-client"
export const initWebsocket = () => {
const { inBuilder, location } = get(builderStore)
2022-08-22 19:24:34 +02:00
// Only connect when we're inside the builder preview, for now
2022-08-19 12:09:20 +02:00
if (!inBuilder || !location) {
return
}
2022-08-22 19:24:34 +02:00
// Initialise connection
2022-08-19 12:09:20 +02:00
const tls = location.protocol === "https:"
const proto = tls ? "wss:" : "ws:"
const host = location.hostname
2022-08-23 09:48:16 +02:00
const port = location.port || (tls ? 443 : 80)
2022-08-19 12:09:20 +02:00
const socket = io(`${proto}//${host}:${port}`, {
2022-08-22 19:24:34 +02:00
path: "/socket/client",
2022-08-19 12:09:20 +02:00
})
2022-08-22 19:24:34 +02:00
// Event handlers
2022-08-19 12:09:20 +02:00
socket.on("plugin-update", data => {
builderStore.actions.updateUsedPlugin(data.name, data.hash)
})
}