#new WebSocket, possible?

1 messages · Page 1 of 1 (latest)

stark drum
#

Hi all! I'm looking porting an existing page that uses web sockets.

Do I have to use Script and dangerouslySetInnerHTML to achieve a connection to web sockets?

As I am unable to use new WebSocket() otherwise I believe.

#

I see in the LiveReload component

dangerouslySetInnerHTML: {
      __html: js`
                function remixLiveReloadConnect(config) {
                  let protocol = location.protocol === "https:" ? "wss:" : "ws:";
                  let host = location.hostname;
                  let socketPath = protocol + "//" + host + ":" + ${String(port)} + "/socket";

                  let ws = new WebSocket(socketPath);
                  ws.onmessage = (message) => {
                    let event = JSON.parse(message.data);
                    if (event.type === "LOG") {
                      console.log(event.message);
                    }
                    if (event.type === "RELOAD") {
                      console.log("💿 Reloading window ...");
                      window.location.reload();
                    }
                  };
                  ws.onopen = () => {
                    if (config && typeof config.onOpen === "function") {
                      config.onOpen();
                    }
                  };
                  ws.onclose = (error) => {
                    console.log("Remix dev asset server web socket closed. Reconnecting...");
                    setTimeout(
                      () =>
                        remixLiveReloadConnect({
                          onOpen: () => window.location.reload(),
                        }),
                      1000
                    );
                  };
                  ws.onerror = (error) => {
                    console.log("Remix dev asset server web socket error:");
                    console.error(error);
                  };
                }
                remixLiveReloadConnect();
              `
    }
#

But, when I drop in our otherwise working websocket url, it fails to connect

#

any ideas?

#

My issue may be a typo somewhere, troubleshooting a bit because swapping in wss://socketsbay.com/wss/v2/2/demo/ for my web socket domain worked

molten wasp
#

yeah, I'm using the Websocket library to connect to appsync just fine

#
import { useContext, useEffect, useState } from "react";
import * as websocket from "websocket";
import { websocketService } from "./Websocket.service";
import { v4 as uuidv4 } from "uuid";
import { RootContext } from "~/routes/auction";

const payload = encodeURIComponent("e30=");
let indyClient: any;

export const useIndySubscription = (query: string, variables: any) => {
    const [message, setMessage] = useState<any>(undefined);
    const [messageId, setMessageId] = useState(uuidv4());
    const rootContext = useContext(RootContext);

    if (!indyClient) {
        indyClient = websocket.w3cwebsocket(
            `wss://${getRealtimeHost()}/graphql?header=${encodeTokens()}&payload=${payload}`,
            "graphql-ws"
        );
    }
#

well, not using new Websocket here so maybe not quite the same? But still a wss so shouldn't matter?

undone relic
clever tree
#

Did you find any solution for this using “new Websocket”?