Integrate WebSocket In A Basic React Application

How to use "Create React App" with WebSocket.

Create React App is an officially supported way to create single-page React applications. It offers a modern build setup with no configuration.

Note: Node.js is required to use create-react-app.

If you don’t have Node installed on your machine refer here.

Quick Start

Open your terminal in the directory you would like to create your application.

Run this command to create a React application named first-app.

create-react-app will set up everything you need to run a React application.

Note: If you’ve previously installed create-react-app globally via npm install -g create-react-app, it is recommended to uninstall the package using npm uninstall -g create-react-app to ensure that npm always uses the latest version.

(npx comes with 5.2+ and higher)

Then open http://localhost:3000/ to see your app.

Creating an App

You’ll need to have Node >=10 on your local development machine (but it’s not required on the server). You can use nvm  (macOS/Linux) or nvm-windows to switch Node versions between different projects.

To create react app, you can also use npm like

npm init <initializer> is available in npm 6+

Selecting a template

You can also start a new app with template by appending –template[template-name]  to the creation command.

If you don’t specify the template, it will use the default template shown in the overview section.

You can find a list of available templates by searching for “cra-template -*” on npm .

Custom Templates documentation describes how you can build your own template.

Selecting a package manager

If you have yarn installed, but would prefer to use npm, you can append –use npm to the creation command. For example:

Installing PieSocket

Install PieSocket NodeJs SDK using NPM to include WebSocket capabilities into your application.

Initialise PieSocket class

Use the following code snippet to initialise PieSocket into your application.

Create an account on piesocket.com and generate your API Keys in apiKey section.

You will have your unique clusterId, apiKey, and secret after generation, which you can integrate into your project as shown above.

To read more about PieSocket documentation and how to use its managed WebSocket API for adding real-time features into your application refer here.

Scripts

Inside the newly created project, you can run some built-in commands:

npm start

 Runs the app in development mode. Open http://localhost:3000 to view it in the browser.

The page will automatically reload if you make changes to the code. You will see the build errors and lint warnings in the console.

npm test

Runs the test watcher in an interactive mode. By default, runs tests related to files changed since the last commit.

Read more about testing.

npm build

Builds the app for production to the build folder. It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.