Skip to content

Instantly share code, notes, and snippets.

@witcheslive
Last active April 7, 2023 21:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save witcheslive/dbbed7644ad6a639a3d1d6b59a8c9597 to your computer and use it in GitHub Desktop.
Save witcheslive/dbbed7644ad6a639a3d1d6b59a8c9597 to your computer and use it in GitHub Desktop.
GET WEB STREAMING WORKING ON YOUR MASTODON INSTANCE NOW!!!!

Do you see three dots everywhere on your Mastodon instance?

Is the console on your instance flodded with

WebSocket connection to 'wss://yourinstance.website/api/v1/streaming/?stream=public:local&access_token=blahblahblah' failed: Error during WebSocket handshake: Unexpected response code: 502

WELL FOR SOME REASON NONE OF THE MASTODON SETUP GUIDES TELL YOU HOW TO SET THIS UP EVEN THOUGH ITS SUPER EASY AND ITS OBNOXIOUS AF

You need to do two things:

  1. Generate a VAPID key
  2. Get your nginx to serve up /api/v1/streaming correctly

The VAPID key goes in your .env.production file in your mastodon root folder. Thankfully it's very nice and tells you how to generate the keys!

# Generate with `RAILS_ENV=production bundle exec rake mastodon:webpush:generate_vapid_key` task (`docker-compose run --rm web rake mastodon:webpush:generate_vapid_key` if you use docker compose)

So yeah depending on if you're dockerized or not, run the appropriate command and fill out VAPID_PRIVATE_KEY and VAPID_PUBLIC_KEY, E Z C L A P

Now you have to set up your nginx. Have a block in said nginx configuration that looks like this as a location block probably after your / location block:

location /api/v1/streaming {
  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header X-Forwarded-Proto https;
  proxy_set_header Proxy "";

  proxy_pass http://127.0.0.1:4000;
  proxy_buffering off;
  proxy_redirect off;
  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection $connection_upgrade;

  tcp_nodelay on;
}

Note that I used 127.0.0.1 instead of localhost because my server has some weird issues where it isnt translating localhost correctly with ipv6 and whatever, this just works.

Once you reload your nginx config and restart/rebuild your mastodon server, streaming should now work and your instance will run much more smoothly with toots coming in as they do! Yay!

@Rudi9719
Copy link

Thank you!

@Ameobea
Copy link

Ameobea commented Apr 7, 2023

Worked great - tyvm!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment