Mitrahsoft
  • SERVICES
    • OUR SERVICES
      • SOFTWARE DEVELOPMENT
      • MOBILE DEVELOPMENT
      • BLOCKCHAIN DEVELOPMENT
      • SOFTWARE TESTING
      • CLOUD & DEVOPS SERVICES
      • PRODUCT DEVELOPMENT
      • OFFSHORE DEVELOPMENT
      • SOCIAL MEDIA MARKETING
      TECHNOLOGIES
      • COLDFUSION DEVELOPMENT
      • REACTJS DEVELOPMENT
      • VUEJS DEVELOPMENT
      • ANGULAR DEVELOPMENT
      • NODEJS DEVELOPMENT
      • PYTHON DEVELOPMENT
      • GOLANG DEVELOPMENT
      • GOLANG CORPORATE TRAINING
      COLDFUSION EXPERTISE
      • MURACMS EXPERTISE
      • COLDFUSION APP MIGRATION
      • COLDFUSION HOSTING & SUPPORT
      • CF LEGACY APP MAINTENANCE
      • COLDFUSION REST API
      • ECOMMERCE / SHOPPING CART SOLUTIONS
      • PRESIDECMS EXPERTISE
      MOBILE DEVELOPMENT
      • REACT NATIVE DEVELOPMENT
      • FLUTTER DEVELOPMENT
      • IONIC DEVELOPMENT
      • PHONEGAP DEVELOPMENT
  • ABOUT US
    • ABOUT MITRAHSOFT
    • OUR CLIENTS
    • TESTIMONIALS
    • PORTFOLIO
    • CAREERS
  • CONTACT US
  • BLOG
Adobe Solution PartnerLucee Solution Partner
Adobe Solution PartnerLucee Solution Partner
Adobe Solution PartnerLucee Solution Partner
  • OUR SERVICESSOFTWARE DEVELOPMENTMOBILE DEVELOPMENTBLOCKCHAIN DEVELOPMENTSOFTWARE TESTINGCLOUD & DEVOPS SERVICESPRODUCT DEVELOPMENTOFFSHORE DEVELOPMENTSOCIAL MEDIA MARKETING
    TECHNOLOGIESCOLDFUSION DEVELOPMENTREACTJS DEVELOPMENTVUEJS DEVELOPMENTANGULAR DEVELOPMENTNODEJS DEVELOPMENTPYTHON DEVELOPMENTGOLANG DEVELOPMENTGOLANG CORPORATE TRAINING
    COLDFUSION EXPERTISEMURACMS EXPERTISECOLDFUSION APP MIGRATIONCOLDFUSION HOSTING & SUPPORTCF LEGACY APP MAINTENANCECOLDFUSION REST API ECOMMERCE / SHOPPING CART SOLUTIONSPRESIDECMS EXPERTISE
    MOBILE DEVELOPMENTREACT NATIVE DEVELOPMENTFLUTTER DEVELOPMENTIONIC DEVELOPMENTPHONEGAP DEVELOPMENT
  • ABOUT MITRAHSOFTOUR CLIENTSTESTIMONIALSPORTFOLIOCAREERS
  • CONTACT US
  • BLOG

WebSocket In React/Node And Steps To Resolve EC2 Polling Issue

HomeBlogWebSocket In React/Node And Steps To Resolve EC2 Polling Issue
ReactJSNodeJSAWS

WebSocket In React/Node And Steps To Resolve EC2 Polling Issue

M
Post by MitrahsoftPublished: Aug 27, 2019

We're going to cover how to use WebSockets to create real-time web applications with React and Node JS and fix EC2 polling issues in AWS.

WebSockets

WebSockets enable the server and client to send messages to each other at any time, after a connection is established, without an explicit request by one or the other.

WebSockets convert their HTTP connection to a WebSocket connection. In other words, a WebSocket connection uses HTTP only to do the initial handshake (for authorization and authentication), after which the TCP connection is utilized to send data via its own protocol.

Socket Polling

WebSocket Process

WebSocket provides the connection between the client and server. This enables both parties to send messages to each other at any time.

The client makes the WebSocket connection using a WebSocket handshake. The WebSocket handshake is a bridge from HTTP to WebSockets.

The client requests an HTTP connection with an Upgrade header. This header informs the server that the client wants a WebSocket connection.

Client Request Header

http
GET ws://localhost:8080/ HTTP/1.1
Origin: http://localhost:8080
Connection: Upgrade
Host: localhost:8080
Upgrade: websocket
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13

The server establishes the connection if the server supports the socket protocol. This connection is made by sending an upgrade header as a response.

Server Response Header

http
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=

WebSocket in React and NodeJS

Now we will implement a WebSocket connection with a React client and NodeJS server using Socket.IO.

Server Socket Connection

Installation

bash
npm i express -S
npm i http -S
npm i socket.io
javascript
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

io.on('connect', function (socket) {
    console.log('connected');

    socket.emit('event', 'Hi from socket server');

    socket.on('disconnect', function () {
    });
});

http.listen(8081, () => {
    console.log('Server listening on ws');
});

Client Socket Connection

Installation

bash
npm i socket.io-client -S

App.js

javascript
this.socket = io.connect('http://localhost:8081');
this.socket.on('connect', function () {
    console.log('Websocket Connected with App');
});
this.socket.on('event', (msg) => {
    console.log('msg', msg);
});
this.socket.on(`${this.user}`, (event) => {
    this.newMessage(event);
});
this.socket.on('disconnect', function (evt) {
    console.log('err', evt);
});

Connect Socket in Specific Path

javascript
var io = require('socket.io')(http, {
    path: '/socket.io'
});
javascript
this.socket = io.connect('http://localhost:8081', {
    path: '/socket.io'
});

This websocket connection working fine with local. Now I like to move it to production. So I've uploaded the server code in AWS Elastic Beanstalk.

The websocket successfully connected in http. But I've faced polling issue in https (SSL port). The web client connects but never upgrades, and just stays in polling mode.

Polling

Polling

When there is no proper connection between the Socket.IO server and client, the Socket.IO client package makes frequent HTTP requests to fetch data instead of using the WebSocket protocol.

How to Fix the Polling Issue?

We need to change the proxy header configuration in EC2 to fix this issue. Follow the steps below.

  1. Create a Key Pair using Amazon EC2. Click here
  2. Add the created key pair to your Elastic Beanstalk application configuration.Keypair ConfigAdd Key to Eb
  3. Convert the downloaded .pem file into a .ppk file by Using the below link. Click here
  4. Open putty and select session. Copy the EC2 Public DNS (IPv4) url and paste it in hostname. (EC2 Public DNS (IPv4) url - Open AWS console and select EC2 in services. Open running instances and select your application. Copy the Public DNS in the description block.)Ec2 Putty
  5. Select the created private key file (.ppk) and click open. It opens a putty window.Puttykey Selection
  6. Give username in putty window.
  7. Edit nginx config using below comments.
nginx
Edit nginx.conf
a) cd /etc/nginx/
b) cat nginx.conf  (This  command display the nginx config)
c) sudo vim nginx.conf (This  command display the nginx config with edit option) and type 'i' to edit the config.
d) Add the below lines in nginx.conf

  map $http_upgrade $connection_upgrade {
    default     "upgrade";
  }

e) Press Esc and type :w to save these changes.

Edit 00_elastic_beanstalk_proxy.conf
a) cd /etc/nginx/conf.d/
b) sudo vim 00_elastic_beanstalk_proxy.conf
c) Replace the below lines in 00_elastic_beanstalk_proxy.conf
  location / {
    proxy_pass          http://127.0.0.1:8081;
    proxy_http_version  1.1;
    proxy_set_header    Connection          $connection_upgrade;
    proxy_set_header    Upgrade             $http_upgrade;
    proxy_set_header    Host                $host;
    proxy_set_header    X-Real-IP           $remote_addr;
    proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
  }

d) Press Esc and type :w to save these changes.
  1. Restart the nginx by using sudo service nginx restart.

Tags

ADYENAMAZON-SESANDROIDAPACHEAPACHE-JMETERAWSBARCODE-SCANNERCOLDBOXCOLDFUSIONCOLDFUSION-API-INTEGRATIONCOLDFUSIONBUILDERECHARTSEVENT-GATEWAYFFMPEGFW1JQUERYJSOUPLUCEEMURACMSMURACMS-THEMENODEJSONESIGNALOSSPAYFLOW-PROPAYMENT-GATEWAYPAYPALPRESIDECMSPUPPETEERRAILORAZUNAREACTJSREACTNATIVERESTSASS-COMPILATIONSEMANTIC-UISUBLIMETEXTTINYMCETWILIOYUI-LIBRARY

Archives

JAN 20DEC 19AUG 19JUL 19JUN 19MAY 19APR 19MAR 19FEB 19JAN 19DEC 18NOV 18OCT 18MAR 16NOV 15SEP 15MAY 15MAY 14OCT 13JUN 13MAY 13APR 13MAR 13FEB 13JAN 13

Follow us

Mitrahsoft
United StatesUnited States

Hurst, Texas, United States

+1 (817) 606-8684usa-sales@mitrahsoft.com

IndiaIndia

Head Office

126G2/5, Thiru malai nagaram,
Kovilpatti - 628 501

Madurai

1/B, 2nd & 3rd floor, GV Complex,

Bye Pass Road, SS Colony,

Madurai - 625 016

Coimbatore

2nd Floor, Sri Narmadha Towers,

Murugan Nagar Rd, Thoppampatti Pirivu,

 K. Vadamadurai, Coimbatore - 641 017

+91 9092480924

business@mitrahsoft.com

ColdFusion
  • eCommerce / Shopping Cart Solutions
  • Payment Gateway Integrations
  • Shipping API Integrations
  • ColdFusion Development Services
  • REST API Development
  • MuraCMS Plugin Creation & Personalization
Technology Services
  • ReactJS Development
  • VueJS Development
  • AngularJS Development
  • NodeJS API Development
  • Python Development
  • Golang Development
  • React Native Development
  • D3.JS data Visualization
Other Services
  • Software Development
  • Mobile Development
  • Blockchain Development
  • Cloud & Devops Services

Connect with MitrahSoft

Please fill out this field
Please fill out this field
Please fill out this field
Please fill out this field
Please fill out this field
Please fill out this field

© 2026 All Rights Reserved. MitrahSoft Solutions Pvt Ltd

Home|About Us|Careers