This article is about fixing WebSocket connection to 'wslocalhost3000ws' failed in Facebook Create React App
  • 18-Jan-2023
Lightrun Team
Author Lightrun Team
Share
This article is about fixing WebSocket connection to 'wslocalhost3000ws' failed in Facebook Create React App

WebSocket connection to ‘ws://localhost:3000/ws’ failed in Facebook Create React App

Lightrun Team
Lightrun Team
18-Jan-2023

Explanation of the problem

The issue is related to a basic React application that is not working properly due to a hardcoded websocket connection to port 3000. This problem appears to be related to running the application in a container, but additional issues have been discovered even when running the application locally.

The error message that appears is:

WebSocket connection to 'ws://localhost:3000/ws' failed:

This error is caused by the React application trying to connect to a websocket on port 3000, but it is unable to establish the connection. This can be due to the port being blocked or not available. The user has tried using port 3000 but the error still persists and the application does not work.

The solution proposed is to change the hardcoded websocket port in the application’s configuration. This can typically be done by modifying the Webpack development server (WDS) configuration. This can be done by updating the webpack.config.js file.

devServer: {
   port: 3000,
   ...
}

In this example, the port is hardcoded to 3000, but it can be modified to a different port number. Additionally, the user suggests that the problem may be related to the environment where the application is running, such as the system, npm, yarn or node version. It’s important to check the system requirements and ensure that the application is compatible with the environment.

Troubleshooting with the Lightrun Developer Observability Platform

Getting a sense of what’s actually happening inside a live application is a frustrating experience, one that relies mostly on querying and observing whatever logs were written during development.
Lightrun is a Developer Observability Platform, allowing developers to add telemetry to live applications in real-time, on-demand, and right from the IDE.

  • Instantly add logs to, set metrics in, and take snapshots of live applications
  • Insights delivered straight to your IDE or CLI
  • Works where you do: dev, QA, staging, CI/CD, and production

Start for free today

Problem solution for WebSocket connection to ‘ws://localhost:3000/ws’ failed in Facebook Create React App

The issue is related to a hardcoded websocket connection to port 3000. This means that the application is trying to connect to a websocket on port 3000, but it is unable to establish the connection. This can be caused by different factors, such as the port being blocked or not available.

One solution is to manually specify the port used in the .env file. This can be done by adding the following line:

WDS_SOCKET_PORT=443

This allows the application to use a specific port for the websocket connection, which can resolve the issue. This is useful for cases where the default port is not available or when there is a misconfiguration in the application. It’s important to check the Create React App documentation for more details on the advanced configuration.

Another solution is to set the WDS_SOCKET_PORT to 0. This will use the window location instead of a specific port. This is an alternative way to resolve the issue, and it’s useful when the port is not known or when the application is running in a dynamic environment. The user suggests that this may be a misconfiguration in Create React App, but it’s worth checking the history in the WDS server to look for other clues.

WDS_SOCKET_PORT=0

It’s important to note that this is a temporary solution, and it’s recommended to check the history in the WDS server and the environment to troubleshoot the problem and find the root cause of the problem.

Other popular problems with Create React App

Problem: CORS issues when making API calls

One of the most common issues with Create React App (CRA) is related to CORS (Cross-Origin Resource Sharing) when making API calls. This happens when the API server and the client (CRA) are running on different domains or ports. The browser blocks the API call due to security reasons.

Failed to load resource: net::ERR_FAILED

This error message is displayed in the browser’s developer console and the API call fails.

Solution:

One solution is to enable CORS on the API server by adding the following headers to the server’s response:

Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, DELETE
Access-Control-Allow-Headers: Content-Type

Another solution is to use a proxy to forward the API call to the API server. This can be done by adding a proxy field to the package.json file:

"proxy": "http://localhost:8000"

Problem: Webpack dev server not working

Another common issue with CRA is related to the Webpack dev server not working properly. This can happen when there are conflicts with other applications running on the same port or when there is a misconfiguration in the Webpack dev server.

Error: listen EADDRINUSE: address already in use :::3000

This error message is displayed in the terminal when starting the dev server.

Solution:

A solution is to specify a different port for the dev server. This can be done by adding the following line to the package.json file:

"scripts": {
  "start": "react-scripts start --port 3000",
}
Another solution is to kill the process that is using the port 3000. This can be done by using the command lsof -i :3000 to find the process id and then use the command kill -9 <PID> to kill the process.

Problem: Hot Reloading not working

Hot Reloading is a feature of CRA that allows developers to see the changes they made in the code without refreshing the browser. However, sometimes this feature stops working and the developers have to refresh the browser to see the changes.

[HMR] Waiting for update signal from WDS...

This message is displayed in the developer console when hot reloading is not working.

Solution:

One solution is to clear the browser’s cache and cookies. This can resolve the issue if the browser is holding on to old files. Another solution is to check the network tab in the browser’s developer console and look for any errors related to the hot-reload.js file. If there is an error, it could be related to a problem with the network connection or a misconfiguration in the Webpack dev server.

A brief introduction to Create React App

Create React App (CRA) is a command line tool for building React applications. It is a tool that helps developers quickly set up a new React project with a basic file structure and a development server. It also includes a number of other features, such as a built-in development server, a Webpack-based build system, and support for JSX and ES6 syntax.

CRA uses Webpack to bundle all the necessary JavaScript, CSS, and other assets into a single file that can be served to the browser. It also includes a development server that automatically reloads the page whenever changes are made to the code. Additionally, CRA also includes a number of other features such as support for linting, testing, and building for production. Overall, Create React App is a great tool for quickly setting up a new React project, and for making development faster and more efficient.

Most popular use cases for Create React App

  1. Create React App can be used for quickly scaffolding a new React project with a basic file structure and development server. This allows developers to get up and running with a new project quickly, without having to manually set up the file structure and configuration.
  2. Create React App can be used to bundle all of the necessary JavaScript, CSS, and other assets into a single file using Webpack. This allows for efficient loading of the application in the browser, and can also be configured to optimize the assets for production use.
  3. Create React App can be used for code splitting, which allows for the lazy loading of components only when they are needed. This can improve the performance of the application by only loading the necessary code at the time it is needed, instead of loading all the code at once. Code splitting in CRA is done by using dynamic import() statement, as an example :
import('./myModule')
    .then(myModule => {
      // Use myModule
    })
    .catch(err => {
      // Handle failure
    });

This allows for the application to only load the specific module when it is needed, rather than loading all modules at once.

Share

It’s Really not that Complicated.

You can actually understand what’s going on inside your live applications.

Try Lightrun’s Playground

Lets Talk!

Looking for more information about Lightrun and debugging?
We’d love to hear from you!
Drop us a line and we’ll get back to you shortly.

By submitting this form, I agree to Lightrun’s Privacy Policy and Terms of Use.