Connecting a Websocket over a Caddy Proxy returns 405 Method Not Allowed with a POST call
hey folks, Im newer to websocket integration so bear with me. I have a spring boot project connecting to a websocket using sockjs-client from my react front end and connecting to a STOMP entrypoint on the spring boot backend. Locally, this works great. However, I am getting a 405 method not allowed when connecting to out railway project. We are using Caddy Proxy to create a proxied URL so this may have something to do with it although Im not sure.
my react call is: let sock = new SockJS('ws'); stompClient = over(sock); stompClient.connect({} ,onConnected, onError); which I get the error: Request URL: https://XXXXXXX.XXXXXXX.com/ws/021/bntqbx12/xhr_streaming?t=1721787651124 Request Method: POST Status Code: 405 Method Not Allowed Remote Address: 34.86.119.124:443 Referrer Policy: strict-origin-when-cross-origin this is because railway only allows for GET and HEAD requests.
Allow: GET, HEAD Content-Length: 0 Date: Wed, 24 Jul 2024 02:20:51 GMT Server: railway on the back end: @Configuration @EnableWebSocketMessageBroker @EnableWebSocket public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { @Override public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) { stompEndpointRegistry.addEndpoint("/ws") .setAllowedOriginPatterns("*") .withSockJS(); } we also have a package json proxy as well for our api requests if that is also important: "proxy": "http://locahost:8081" I can provide more info if you need it. Thanks in advance!
my react call is: let sock = new SockJS('ws'); stompClient = over(sock); stompClient.connect({} ,onConnected, onError); which I get the error: Request URL: https://XXXXXXX.XXXXXXX.com/ws/021/bntqbx12/xhr_streaming?t=1721787651124 Request Method: POST Status Code: 405 Method Not Allowed Remote Address: 34.86.119.124:443 Referrer Policy: strict-origin-when-cross-origin this is because railway only allows for GET and HEAD requests.
Allow: GET, HEAD Content-Length: 0 Date: Wed, 24 Jul 2024 02:20:51 GMT Server: railway on the back end: @Configuration @EnableWebSocketMessageBroker @EnableWebSocket public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { @Override public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) { stompEndpointRegistry.addEndpoint("/ws") .setAllowedOriginPatterns("*") .withSockJS(); } we also have a package json proxy as well for our api requests if that is also important: "proxy": "http://locahost:8081" I can provide more info if you need it. Thanks in advance!
22 Replies
Project ID:
e48131f7-096a-4b0a-8733-ecc43f93da7b
e48131f7-096a-4b0a-8733-ecc43f93da7b
why the need for caddy to begin with?
also, please dont obfuscate things like this
xxxxxxx.xxxxxxx.com
its not helpfulsorry, ya no worries --> https://assignments-dev.coderscampus.com/ws/021/bntqbx12/xhr_streaming?t=1721787651124
Request Method:
POST
Status Code:
405 Method Not Allowed
Remote Address:
34.86.119.124:443
Referrer Policy:
strict-origin-when-cross-origin
Assignment Submission
Web site created using create-react-app
I believe we were using caddy to generate a public domain
Railway generates a public domain for you with SSL
and for websocket connections wouldnt you send a GET request to
wss://assignments-dev.coderscampus.com/ws/021/bntqbx12/xhr_streaming?t=1721787651124
hmmm, Im getting the error
why would wss not be allowed for a websocket endpoint
does the request need to be a post type? are you sending data in the body?
hmm maybe its the library Im using? Im not exaclty sure, using sock-js-client
nope, just trying to connect
either way, why caddy?
send the caddyfile so i can get an idea of what you are trying to do
ya for sure,
this just serves a frontend website
i know that, because i wrote that
ooo nice! Thanks for writing it! We were just experimenting, my co worker and I and we came on some examples, so far its working as it should, but you think its overkill? Maybe messing with the domain?
no one has ever had issues with that caddyfile that i know of, using caddy to serve a frontend website is going to be by far the best way to do it on railway in my experience
so the issue is elsewhere
can you send a screenshot of your project canvas please
yup for sure, is this it here?
okay and what is the caddy proxy for?
i wrote that too, but i see a lot of people using it when they actually dont need to be
well, it looks like all we're doing here is connecting our front and back end under one domain
is that strictly needed?
honestly cant say for sure, Id have to ask my co worker but is there an alternative you think instead? By removing the caddy proxy, you think it should clear the air?
oop, holdup, I may have sent you the wrong caddy file -->
i think there are some misunderstandings on your side as to why you need the proxy, i would eliminate it and go from there
have your frontend call the backend's domain directly, you of course will need to setup cors on your backend but thats a 5 minute tutorial and done
okay ya for sure! I'll try this and get back to you. Thanks for your help!
no problem
oh and the reason you where getting
405 Method Not Allowed
is because the path /ws/021/bntqbx12/xhr_streaming
is a path in the frontend.
my proposed solution still stands though