Websocket connection failing

Hello,

I’ve been trying to get websockets working, but coming up against some curious errors.

I am connecting to the Mailsac websocket server in react, using react-use-websocket. Initially, I tested with echo.websocket.org. Works great.

Minimum reproducible code looks like:

import useWebSocket, { ReadyState } from 'react-use-websocket';

export const WsComponent = () => {
  const [socketUrl, setSocketUrl] = useState('');

  const { lastJsonMessage, readyState } = useWebSocket(`wss://echo.websocket.org`, {
    onOpen: () => console.log('Opened')
  });

  useEffect(() => {
    if (lastJsonMessage !== null) {
      console.log(lastJsonMessage);
    }
  }, [lastJsonMessage]);

  const connectionStatus = {
    [ReadyState.CONNECTING]: 'Connecting',
    [ReadyState.OPEN]: 'Open',
    [ReadyState.CLOSING]: 'Closing',
    [ReadyState.CLOSED]: 'Closed',
    [ReadyState.UNINSTANTIATED]: 'Uninstantiated',
  }[readyState];

  return <p>{connectionStatus}</p>;
}

:white_check_mark: This connects.

:white_check_mark: Then I went over to https://sock.mailsac.com/, and tested my key and email there. This web page was able to connect to Mailsac websockets.

So now it’s time to connect to Mailsac in React!

import useWebSocket, { ReadyState } from 'react-use-websocket';

const apiKey = 'k_<my_key>'; // redacted
const email = '[email protected]'; // not my email
const socketUrl = `wss://sock.mailsac.com/incoming-messages?key=${apiKey}&addresses=${email}`;

export const WsComponent = () => {

  const { lastJsonMessage, readyState } = useWebSocket(socketUrl, {
    onOpen: () => console.log('Opened'),
    shouldReconnect: (closeEvent) => true,
    reconnectAttempts: 10,
    reconnectInterval: (attemptNumber) =>
      Math.min(Math.pow(2, attemptNumber) * 1000, 10000),
  });

  const connectionStatus = {
    [ReadyState.CONNECTING]: 'Connecting',
    [ReadyState.OPEN]: 'Open',
    [ReadyState.CLOSING]: 'Closing',
    [ReadyState.CLOSED]: 'Closed',
    [ReadyState.UNINSTANTIATED]: 'Uninstantiated',
  }[readyState];

  return <p>{connectionStatus}</p>;
}

:x: Fails constantly with console error:
react-dom-global:react-dom:2 WebSocket connection to 'wss://sock.mailsac.com/incoming-messages?key=k_<my_key>&[email protected]' failed:

Is someone able to please reproduce this, and help sanity check me? Clearly I’m doing something wrong, but I cannot for the life of me figure out what.

1 Like

Interestingly, attempting to connect from another websocket testing website fails.

Thanks for all the details. Have you tried viewing the source of the mailsac websocket test page?

https://sock.mailsac.com

It’s written in plain JS to help people write web socket integrations. If you view source on the HTML, at the bottom you’ll see the script tag. Nothing fancy is in there.

Yep, tried the Mailsac one. That’s the only website that will connect to the Mailsac websocket.

I’ve tried to connect from:

Neither of these can connect to the Mailsac web socket URL (+ key and addresses)

I saved the webpage at https://sock.mailsac.com and ran it locally, adjusting some of the JS so that it would connect to the correct websocket URL, and I get the same error:

Is the Mailsac websocket server configured to allow connections from other origins?

Hi @hansontools,
You’re right, that is almost certainly the issue. Mailsac has not had anyone ask about CORS support for web sockets before. They’ve only used web sockets from servers. Mailsac.com’s REST API does work with CORS, so I would expect we can get this working, but it will take a little thought and work.

All good. I’ll set up an endpoint for a Mailsac webhook and pipe that into my own WS server :+1:

Do Mailsac webhooks calls have an hmac signature or something we can use to verify the payload is from Mailsac?

Hi @hansontools - sorry I missed your last message.

We have enabled CORS for sock.mailsac.com.

For Webhooks, there is not currently a signature added.