does socket.io server need to be seperate from backend when deploying? - node.js

I am building react app. I have my client folder, and my backend folder that contains all my mongo db models, routes, functions etc...
I know realize that my app needs to use socket.io
My frontend is on localhost:3000 and my backend is on localhost:5000
My understanding is that socket.io needs its own port.
Does this mean when I deploy to heroku I need to deploy a backend server, frontend server, and a socket.io server?

My understanding is that socket.io needs its own port.
This is incorrect. socket.io can use the same port as your backend just fine. Incoming requests to create a socket.io connection can be distinguished from other web requests via a custom header that the underlying webSocket connection protocol uses. This allows socket.io/webSocket and your http server to use the exact same port.
Does this mean when I deploy to heroku I need to deploy a backend server, frontend server, and a socket.io server?
No. You can still just have frontend server and backend server and the backend server can handle both your backend requests and the socket.io connections.

Related

How to make a secure communication for NodeJs server and React.js as client (static hosting) using EC2 as a middleman for routes/forwarding request?

How do I make a secure communication for NodeJs server and React.js as client using EC2 as a middleman for routes/forwarding request?
I am using Socket.IO. And I want to use EC2 server as a middleman for routing/forwarding all requests from nodejs server to react.js client.
Also including the AWS Certificate.

How build to deploy node app to http server

Is there any way to use express or other nodejs librarys to to design an api and deploy it pasting files as a resource path in a web server just like react.
My web server just listen on ports 80 and 443.
I have pages on domain.com/page1 domain.com/page2.
and i want to deploy an api listening on domain.com/page3 but i cant install a nodejs server and proxy requests to page3 to it.
Thanks a lot
There can be only one web server listening on a given port. You can directly "listen to a path", only a port.
So, if you already have an existing web server running on ports 80 and 443, then these are your options.
Modify your existing web server to add your API server code to it so it can handle requests for http://yourdomain.com/page3 and https://yourdomain.com/page3 directly with your API.
You can add some middleware to your existing web server to make it a proxy for http://yourdomain.com/page3 that will redirect requests to your http://yourdomain.com:3000/page3 Express-based API server.
Run your own Express-based API server on your own separate port and access the API server directly via the separate port such as http://yourdomain.com:3000/page3.
You can install a proxy such as nginx in front of your existing web server and have it redirect incoming requests to http://yourdomain.com/page3 and https://yourdomain.com/page3 to the separate port that your Express-based API server is running on.

What is recommended way to set up a web server?

I have a server and I am designing some web applications. I use React as frontend framework and Rust Actix as backend framework. The backend program listens at 8000 port, and can be reached by my_domain:8000/api/xxxx.
I think I can use a web server listening at 80 port, such that if client is requesting /, then returning the frontend page, if client is requesting /api, then redirect the request to 8000 port of localhost.
My problems are:
Is the above way recommended in the modern web application design? Are there any other ways of hosting both frontend and backend application in the server?
What web server can I use? Apache, Nginx, or manually write a web server?
I use two docker containers to contain the frontend and backend app. Do I need to dockerize the web server as well?

socket.io on AWS EC2 Instance

I have an express node server running that is a backend for a REST API and a Websockets (for chatting feature) while my client is React.js
When I deploy my server to an AWS EC2 instance with security groups set up, I am able to make http api calls, but my socket.io connection doesn't work. I have tested the server locally on my localhost, which works.
I think this does something with the proxy in the package.json because testing locally when I change the proxy to my EC2 instance public ip while keeping my socket.io connection connected to my localhost it does not work.
I am not getting any "connection refused" errors.
So my question is how does the react.js proxy effect the socket.io connection?

Connection between frontend server (Node JS and Angular JS) with backend server (.NET API)

I am having an issue with the connection between my frontend server and the backend server. Both solutions are hosted as Web Apps in Azure. The frontend solution is an Angular solution with a Node server, and the backend solution is an API in .NET.
Both solutions are working good without problems, but I want to filter the connections with the API. I mean, I want to set that the IP from the web app in where I hosted the frontend solution is the only IP which is able to connect to the API. But the IP that the API receives is the IP from the client, not from the server in where the web app is hosted, so I need to know what is happening there. I thought that if an user connects to the frontend solution, and the frontend solution calls to an API, the API should receive the IP from the server in where is hosted the frontend solution, not the IP from the client.
I am using Express and this would be the server.js file in Node:
const express = require('express');
const app = express();
app.use(express.static(__dirname + '/public'));
app.listen(8080);
Something like:
User (IP: X) -----> Frontend Solution (IP: Y) -----> Backend Solution (IP: Z)
Backend Solution should allow the IP Y and deny the IP X.

Resources