h3poteto / megalodon

Fediverse API client library for node.js and browser
https://h3poteto.github.io/megalodon/
MIT License
250 stars 33 forks source link

Invalid "instanceof" operand URL when using WebSocket on client #1576

Open CPlusPatch opened 1 year ago

CPlusPatch commented 1 year ago

Hello, I am using Next.js to make an app and I am trying to connect to my account using WebSocket.

Here is my code (frontend React)

export const HomeFeed = () => {
    const [posts, setPosts] = useState<Entity.Status[]>([]);

    useEffect(() => {
        const client = generator(
            "pleroma",
            "wss://fedi.cpluspatch.com",
            process.env.NEXT_PUBLIC_ACCESS_TOKEN,
        );

        const stream: WebSocketInterface = client.userSocket()

        stream.on("connect", () => {
            console.log("connect");
        });

        stream.on("parser-error", (err: Error) => {
            console.error(err);
        });
    }, []);

    return ...
};

Here is my Webpack config:

webpack: (config) => {
        config.resolve.fallback = {
            net: false,
            tls: false,
            dns: false,
            zlib: false,
            fs: false,
            stream: require.resolve('stream-browserify'),
            events: require.resolve('events'),
            buffer: require.resolve('buffer'),
            url: require.resolve('url'),
            http: require.resolve('stream-http'),
            https: require.resolve('https-browserify'),
            crypto: require.resolve('crypto-browserify'),
            querystring: require.resolve('querystring-es3'),
            os: require.resolve('os-browserify/browser'),
            assert: require.resolve('assert')
        }

        return config;
    }

The error I am getting is "TypeError: invalid 'instanceof' operand URL" at line const stream: WebSocketInterface = client.userSocket()

Here is my stack trace: https://pastebin.com/v5hkQN0Y

Am I doing something wrong?

h3poteto commented 1 year ago

Hmm, I'm not sure. Your code seems good.

CPlusPatch commented 1 year ago

This has happened several times with different projects