1
0
Fork 0
mirror of synced 2024-09-20 03:08:18 +12:00
budibase/packages/client/src/websocket.js
2022-08-22 18:24:34 +01:00

27 lines
776 B
JavaScript

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)
// Only connect when we're inside the builder preview, for now
if (!inBuilder || !location) {
return
}
// Initialise connection
const tls = location.protocol === "https:"
const proto = tls ? "wss:" : "ws:"
const host = location.hostname
const port = location.port || (tls ? 433 : 80)
console.log(`${proto}//${host}:${port}`)
const socket = io(`${proto}//${host}:${port}`, {
path: "/socket/client",
})
// Event handlers
socket.on("plugin-update", data => {
builderStore.actions.updateUsedPlugin(data.name, data.hash)
})
}