Socket.io, connect to socket inside docker container - node.js

My app is using websocket with Socket.io library. On the fronted, I open a connection to a backend socket like this :
io.connect(window.location.origin)
Everything is working fine when my backend is running on my computer. But when I run it inside a docker container, I get this error on my client:
WebSocket connection to 'ws://localhost/socket.io/?EIO=3&transport=websocket' failed: Invalid frame header

Figured out that I must make the socket listen on '0.0.0.0'

Related

Nodejs app in Docker not accessible on localhost:3000

I am trying to run a Node application in a Docker container. The installation instructions specified that after adding host: '0.0.0.0' to config/local.js and running docker-compose up the app should be accessible at localhost:3000, but I get an error message in the browser saying "The connection was reset - The connection to the server was reset while the page was loading."
I have tried to add the host: '0.0.0.0' in different places, or remove it entirely, access https://localhost:3000 as opposed to at http, connecting to localhost:3000/webpack-dev-server/index.html, etc.
What could be going wrong?

How connect to the mongodb from localhost using docker on Linux

I have this line of code:
const client = await MongoClient.connect('mongodb://127.0.0.1:27017')
But I cant run http://localhost:3000 because the code is waiting the connection to the database to start listen to the 3000 port
I need to create a connection to the mongodb from localhost using docker, but dont know how

Depending on HOST NAME value for PostgreSQL database in my .env file I get different result

I have created a PostgreSQL with a web server DOCKER CONTAINER and my app running with an API to manage all the endpoints.
The link of my git repo is: https://github.com/JulioValderrama/store-front-project.git
But I am facing problems with the connections with the Database, as depending on the Postgres HOST name value in my .env file one of the two servers (one local running in PORT 4000 and the web server running in the docker container in PORT 3000) will work or will fail.
I have tried the next:
Local server running in PORT 4000
Docker web server running in PORT 3000
HOST "127.0.0.1"
Going to¨:
http://localhost:3000/
Works fine and get response, now when trying to connect it to any of my database API:
http://localhost:3000/products
I get the error:
"Could not get PRODUCTS. Error: Error: connect ECONNREFUSED 127.0.0.1:5432"
Going to:
http://localhost:4000
Works fine and get response, now when trying to connect it to any of my database API:
http://localhost:4000/products
It works! I get the list of all my products!!
HOST "postgres"
I put "postgres" because I read online that you have to name HOST as the postgres docker image created, which is my case. And it works for the remote server.
Going to:
http://localhost:3000
Works fine and get response, then when trying to connect it to database API:
http://localhost:3000/products
It works!! It gives me the list of my products !!
Going to:
http://localhost:4000
Works fine and get response, then when trying to connect it to database API:
http://localhost:4000/products
It gives me the error:
"Could not get PRODUCTS. Error: Error: getaddrinfo ENOTFOUND postgres"
So it seems like there is an error when trying to connect to the database due to the server or the HOST name, I have no idea....
In docker-compose.yaml you have linked your machine 5432 to containers 5432 port.
If you are using docker container and want to reach postgres, use postgres as POSTGRES_HOST value.
If you are running application outside the docker environment use 0.0.0.0, 127.0.0.1.
Make sure you haven't installed postgres locally
brew uninstall postgres

Mongodb crud with an ionic application

I created a database on a remote server. I created a js file to connect to the database using mongoose, express, body-parser.
I made a connection to the remote server with the following command:
mongoose.connect ("mongodb: //176.31.212.51: 27017 / db")
I test the connection and I have a successful connection,
I launched a "listener" to listen on the port as follows:
app.listen (process.env.PORT)
with the port = 3000
I tested and I got an error.
Is there any solution to make a successful connection with the ionic application to do the operations of CRUD?

Node.js and Apache: connection issues

I have installed Node.js with Socket.io on a CentOS server which is running Apache on port 80.
I created a socket test, which justs listens on port 8080.
If I curl the address localhost:8080 from within the server's shell, I get the Socket.io-welcome message. If I have a line like this:
<script src="http://localhost:8080/socket.io/socket.io.js"></script>
Then the browser cannot find the file.
A "solution" was to proxy requests to /nodejs/ to http://localhost:8080/, but this solution did not work for very long.
Is it possible to run the Node.js server when we have Apache installed? Which settings must be changed in order for us to access the url: http://server.com:8080 ? It seems the Node.js only accepts connections from localhost.
Problem is most probably in your node.js program.
It should listen on 0.0.0.0 and not 127.0.0.1 which is local only.
So where you've got something like:
.listen(8080, '127.0.0.1'); // '127.0.0.1' or 'localhost'
You should change it to:
.listen(8080); // or 0.0.0.0
Apache will only interfere if it also uses port 8080 but you should get an error when starting your node app if this is the case.
Also, if you connect to http://localhost in your browser, it will only work if the server is on the same local machine as the browser. Fine for testing I guess.
You'll have to connect to a domain or ip address if you have a hosted server else no browser will find it.
Update:
Your socket.io code also needs to connect correctly:
var socket = io.connect('http://correct.server.com:8080'); // not localhost
and your browser needs to load the javascript file from the correct place:
<script src="http://correct.server.com/socket.io/socket.io.js"></script> // not localhost
This might help with firewall / load balancer issues:
https://github.com/LearnBoost/socket.io/wiki/Socket.IO-and-firewall-software

Resources