(Solved) Websocket connection fails with 'Mattermost on Docker for production use'

I referred to Install Mattermost via Docker — Mattermost documentation to deploy Mattermost locally on Ubuntu (with WSL).

The installation worked fine and I can start Mattermost (without nginx) using

sudo docker compose -f docker-compose.yml -f docker-compose.without-nginx.yml up -d

I created a new user

$ docker exec -ti 478076cfedfe mmctl --local user create --email localbot@**** --username localbot --password ***
Created user localbot

I created a personal access token (Personal Access Tokens — Mattermost documentation) for this user and using that I can make API calls -

$ curl -i -H 'Authorization: Bearer ebg***' http://localhost:8065/api/v4/users/me
HTTP/1.1 200 OK
Content-Type: application/json
Etag: 7.8.14.apy1htzdgibxzeeu1o5u9ysese.1704231806528..0.true.true.0
Expires: 0
Permissions-Policy:
Referrer-Policy: no-referrer
Vary: Accept-Encoding
X-Content-Type-Options: nosniff
X-Request-Id: wsnd5sxoa38ntfd9i8m9frszjr
X-Version-Id: 7.8.14.7.8.14.565e53075911beb8df97f2b871fd3108.false
Date: Tue, 02 Jan 2024 22:51:18 GMT
Content-Length: 747

{"id":"apy1htzdgibxzeeu1o5u9ysese","create_at":1704231568613,"update_at":1704231806528,"delete_at":0,"username":"localbot","auth_data":"","auth_service":"","email":"localbot@***","nickname":"","first_name":"","last_name":"","position":"","roles":"system_user system_user_access_token system_post_all","notify_props":{"channel":"true","comments":"never","desktop":"mention","desktop_sound":"true","desktop_threads":"all","email":"true","email_threads":"all","first_name":"false","mention_keys":"","push":"mention","push_status":"away","push_threads":"all"},"last_password_update":1704231568613,"locale":"en","timezone":{"automaticTimezone":"America/New_York","manualTimezone":"","useAutomaticTimezone":"true"},"disable_welcome_email":false}

Now I want to connect to the Websocket (Mattermost API Reference) and authenticate with this same personal access token. The doc says

To authenticate with an authentication challenge, first connect the WebSocket and then send the following JSON over the connection…

This suggests that the initial connection should just work without issues and then an authentication message needs to be sent. However, this initial connection fails. I tried the following code -

<html>
   <head><title>Welcome to Websocket Test</title>

<body>

<script>
  let websocketUrl = "ws://localhost:8065/api/v4/websocket";
  const ws = new WebSocket(websocketUrl);
  ws.onopen = () => {
     console.log('Websocket opened! Subscribing...');
  }

ws.onmessage = (event) => {
  const message = JSON.parse(event.data);
  console.log("Got message", message);
};

</script>

</body>

</html>

And I always get

WebSocket connection to 'ws://localhost:8065/api/v4/websocket' failed: 

What am I missing?

I have hardly changed any of the configuration and I am simply using the default. The only thing I changed was setting EnableLocalMode to true in config.json, so that I could create users from the command line.

The first thing I always think of with WebSocket issues is Integrations configuration settings — Mattermost documentation

1 Like

@john.oliver thank you for the quick response! Setting the
“Enable cross-origin requests from:” to * fixed the problem.

2 Likes