Welcome! Log In Create A New Profile


Nginx, Docker, React, Express with websocket

Posted by Stanislav 
Nginx, Docker, React, Express with websocket
October 14, 2018 11:32AM
I was developing an app which receives twitter feed in live format and everything worked fine untill i tested the production environment with docker.

The problem i have is that my client (react) does not want to connect to socket connection. (react code at the end)

This is my nginx.conf file

upstream client {
server client:3000;

upstream api {
server api:8080;


listen 80;

location / {
proxy_pass http://client;

location /socket.io {
proxy_pass http://api;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";

location /api{
rewrite /api/(.*) /$1 break;
proxy_pass http://api;
and this my docker compose

version: "3"
restart: always
image: coco/nginx
dockerfile: Dockerfile.dev
context: ./nginx
- "3050:80"
image: coco/server
dockerfile: Dockerfile.dev
context: ./server
- /app/node_modules
- ./server:/app
image: coco/client
dockerfile: Dockerfile.dev
context: ./client
- /app/node_modules
- ./client:/app

In my react container i have the entire code which is responsible for websocket communication but the key problem is to setup the websocket itself with this line of code const socket = socketIOClient("http://localhost:8080");, When it was on localhost it worked fine, and now when i start docker-compose, nginx maps everything to associated with back-end to port 3050. But now even if i write "http://api/" or localhost:3050, it doesnt want to connect directly, so im not sure what to do at this stage.

in my app.js file i have this

// configuring the port which is from config folder
let server = app.listen(config.port, function (err) {
if (err) throw err;
console.log("Server started on port " + config.port);

const io = require('socket.io').listen(server)

require('./routes/routes')(app, io)
In my react component i have a button, and when pressed it executes the following logic

const socket = socketIOClient("http://localhost:3050");
socket.on("connect", () => {
console.log("Socket Connected");
socket.on("tweets", data => {
but it doesnt display anything nor it connects to anything

Pleas help
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 67
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready