If you are encountering a "404 Error during WebSocket handshake" when using ActionCable in Rails 5 on AWS, it's likely caused by misconfigurations in your WebSocket setup or issues with the load balancer or proxy server.
Here are some common reasons for this error and possible solutions:
ActionCable Configuration: Ensure that ActionCable is properly configured in your Rails application. Verify that you have the necessary configuration files, such as
cable.yml, and that your
config/cable.phpfile has the correct settings, including the URL for ActionCable.
WebSocket Protocol and Load Balancer: WebSocket connections require a specific protocol (ws:// or wss:// for secure connections). Some load balancers or proxy servers may not be properly configured to handle WebSocket connections. Ensure that your load balancer or proxy server is correctly configured to pass WebSocket requests to your Rails application.
Proxy and WebSocket Headers: Check if your proxy server strips or modifies WebSocket-specific headers, such as
Connection, during the WebSocket handshake. Some proxies are not WebSocket-aware and may interfere with the WebSocket handshake process. Verify that your proxy server is configured to support WebSocket connections.
Security Groups and Firewall Settings: Make sure your AWS security groups and firewall settings allow incoming WebSocket connections on the specified port (e.g., 80 for HTTP and 443 for HTTPS). Additionally, ensure that the load balancer is correctly configured to forward WebSocket connections to your application instances.
Verify WebSocket URL: Double-check the WebSocket URL used in your client-side code. The WebSocket URL should match the configuration of your ActionCable server. For example, if your Rails application is running on port 3000 and using the
wsprotocol, the WebSocket URL should be
SSL and Certificate Issues: If you are using a secure WebSocket connection (wss://), ensure that your SSL certificate is valid and correctly configured. Any SSL-related errors could cause handshake issues.
Application Load Balancer (ALB): If you are using an Application Load Balancer (ALB) on AWS, make sure that it supports WebSocket connections. By default, ALBs do not support WebSocket connections, but you can enable this feature by configuring your ALB and target groups correctly.
Using ActionCable on Puma Server: If you are using Puma as your application server, ensure that it is properly configured to handle WebSocket connections. You may need to add specific settings to your Puma configuration file.
Check the logs of your Rails application and the load balancer or proxy server for any error messages or clues about the cause of the issue. Additionally, consider using a WebSocket testing tool, such as
wscat, to manually test WebSocket connections to ensure they are working correctly.
By addressing these common issues and verifying your WebSocket configuration, you should be able to resolve the "404 Error during WebSocket handshake" and enable WebSocket connections for your ActionCable in Rails 5 application on AWS.