NuxtN
Nuxt9mo ago
t.f.e

Websocket "unknown" type in typescript app

I have this plugin, and I want to use the send functionallity on components. I also want to add others later on (subscribe to topic etc).
import { defineNuxtPlugin } from "#app";

export default defineNuxtPlugin(() => {
    const ws = new WebSocket("ws://localhost:3000/websocket");

    ws.onopen = () => console.log("Connected to WS");
    ws.onmessage = (message) => {
        const parsedData = JSON.parse(message.data);
        console.log("Received data:", parsedData);
    };

    ws.onerror = (error) => console.log(`Error: ${error}`);
    ws.onclose = () => console.log("Disconnected from WS");

    function send(data: string | object) {
        ws.send(typeof data === "string" ? data : JSON.stringify(data));
    }

    return {
        provide: {
            $ws: { send }
        }
    };
});


But when using it it says it is unknown type? Is the type not transferred using nuxtplugins? See image for example.
image.png
Was this page helpful?