Heroku Node.js WebSocket Server - node.js

The Heroku Dev Center article Using WebSockets on Heroku with Node.js explains how to deploy the Node.js Websocket Test demo application, which uses Express.
However, I'm deploying a Node.js WebSocket chat server that doesn't use Express.
When I try to connect from Terminal with wscat -c ws://my-app.herokuapp.com/1, I get error: Error: unexpected server response (503). And, heroku logs returns code=H14 desc="No web processes running".
Why? How do I fix this?
Note: My Procfile is correct: web: node server.js.

Solution: Delete & recreate the app.
I changed the first line of the Node.js WebSocket chat server to:
var webSocketServer = new (require('ws')).Server({
server: require('http').createServer(function (request, response) {
response.end()
}).listen(process.env.PORT || 5000)
}),
Still didn't work.
Then, I deleted & recreated the Heroku app and deployed. Worked.
Then, I changed the first line back to:
var webSocketServer = new (require('ws')).Server({port: (process.env.PORT || 5000)}),
and redeployed. Still works!

Can you connect with curl -vD to get verbose information on what headers you server is sending?

Related

Unable to connect nodejs api on localhost:5000 on server

I have successfully deployed React app on digitalocean droplet with mongodb.
My server configured with nodejs nginx pm2 and everything is working fine
here is my site
www.xdomain.ml
But I build my react app to connect with the backend nodejs api.
I am using socket.io and axios to connect.
everything is working fine on my local computer but it could not connect on the server.
my socket configuration is like
const socket = io("localhost:5000")
and axios is like
axios.post("http://localhost:5000/delete-domains")
on browser console, showing
polling-xhr.js:198 GET https://localhost:5000/socket.io/?EIO=4&transport=polling&t=NjNh9TP net::ERR_SSL_PROTOCOL_ERROR
what I am missing?
do I need to replace the localhost:5000 to 127.0.0.1:5000 on code and build again?
But the mongodb connection successfully establish by
"mongodb://localhost:27017/api";
please help
Thanks
Try to change
const socket = io("localhost:5000")
to
const socket = io("http://localhost:5000")
Or other advance setting:
const socket = io.connect('http://localhost', {
port: 5000,
reconnect: true
});
const socket = io("localhost:5000")
on this line you are missing "http://" or if you have ssl "https://"
this line should be
const socket = io("http://localhost:5000");

problem in connecting node.js and mongodb

I am building a website using node.js and mongoDB backend. Whenever I start running the server, frontend works completely fine but I am unable to connect to my database. Running mongod command always gives the message
{"t":{"$date":"2020-11-10T10:46:27.043+5:30"},"s:"I","c":"Network","id":23016,
"ctx":"listener","msg":"Waiting for connection","attr":{"port":27017,"ssl":"off"}}
I simultaneously executed npm start from the frontend and backend folders. The npm start from the backend folder gives
[nodemon] starting `node ./server.js`
Listening on port 8080
MongoDB connected
The npm start from the frontend folder gives
App running at:
- Local: http://localhost:8080/
- Network: http://192.168.56.1:8080/
But whenever the open it, it gives
Cannot GET /
Is it because I couldn’t connect to the database? I’m fairly new to this. I can’t seem to understand the reason behind this. Can anyone please help me out?
Cannot GET /
means http://localhost:8080/ has no method defined in your app.
if you are using expressJs
in your server.js
add
var express = require('express')
var app = express()
app.get('/', function (req, res) {
res.send('No More error')
})
Here we are saying: send "No More error" whenever user opens "/" path.
i.e http://localhost:8080/ in your case.

socket.io on openshift with angular-fullstack

I generated a node.js app using Yeomans angular-fullstack generator.
Everything worked fine except the use of socket.io. After uploading my app to openshift using angular-fullstack:openshift, I got informed that I have to:
Openshift websockets use port 8000, you will need to update the client
to connect to the correct port for sockets to work.
in /client/app/components/socket/socket.service: var ioSocket = io.connect('http://my-domain.rhcloud.com/:8000')"
I dont know where to do this. I am using socket.io version 1.0.6 which is shown inside package.json file.
==> app-root/logs/nodejs.log <==
/var/lib/openshift/xxx/app-root/runtime/repo/server/config/socketio.js:41
socket.address = socket.handshake.address.address + ':' +
^
TypeError: Cannot read property 'address' of null
at Namespace. (/var/lib/openshift/xxx/app-root/runtime/repo/server/config/socketio.js:41:46)
at Namespace.EventEmitter.emit (events.js:95:17)
at Namespace.emit (/var/lib/openshift/xxx/app-root/runtime/repo/node_modules/socket.io/lib/namespace.js:205:10)
at /var/lib/openshift/xxx/app-root/runtime/repo/node_modules/socket.io/lib/namespace.js:172:14
at process._tickCallback (node.js:415:13)
DEBUG: Program node server/app.js exited with code 8
DEBUG: Starting child process with 'node server/app.js'
By the way, the app, including socket.io, works fine on my local development machine!
Thanks for any help!
Fall.Guy
My working solution is now:
I changed the creation of the openshift app to "not create a scalable app" (I disabled the -s switch in the yo angular-fullstack:openshift command)
After that, I changed the connection of socket.io to:
var ioSocket = io('http://my-domain.rhcloud.com:8000', {
// Send auth token on connection, you will need to DI the Auth service above
// 'query': 'token=' + Auth.getToken()
});
in the "/client/components/socket/socket.service.js" file.
Socket.io works now fine!
I'm not sure why openshift requires this, but you just need to modify your client code.
In your client side code, you should have something like this:
var socket = io();
You'll apparently need to update it to:
var socket = io('http://mywebsite.com:8000');
It's worth noting that there are plenty of other hosting providers (even free) which don't require you to jump through these hoops.

Redis in Nodejs on Cloud9 IDE: [Error: Auth error: undefined]

Here is my code:
var express = require("express"),
app = express(),
server = require("http").createServer(app),
io = require("socket.io").listen(server),
redis = require("redis"),
env = {PORT: process.env.PORT || 8080, IP: process.env.IP || "localhost"};
client = redis.createClient(env.PORT , env.IP);
client.on("error", function(err) {
console.log(err);
});
server.listen(env.PORT);
console.log("Server started # " + env.IP + ":" + env.PORT);
After trying to run, I received the followings on the console:
Running Node Process
Your code is running at 'http://modified.address.c9.io'.
Important: use 'process.env.PORT' as the port and 'process.env.IP' as the host in your scripts!
info: socket.io started
Server started # modified.ip.address.1:8080
[Error: Auth error: undefined]
I tried establishing the connection, and it connects to the IP and PORT perfectly. However, the error [Error: Auth error: undefined] appears and stops there. I Googled the error, the supports from the IDE I used..., and surprisingly, there are only 7 links to my problems. So I think it may be a hole in my knowledge or it is not really a problem yet a thing I don't know to work it out. All I could pull out from those Google results were (I was not sure) I need to use client.auth(pass) right after creating it. But where should I find the password? When I installed it npm install redis I didn't configure anything and wasn't told to set password whatsoever. So I reach the impasse.
I use Cloud9 IDE (c9.io), and the modules used as shown in the code above.
----With best regards,
----Tim.
I've found out what was wrong.
I did install Redis, but that is a Redis library that acts like a bridge between Redis driver and NodeJS. On Cloud9, I have to manually install Redis, too.
So it would take 2 commands to actually install Redis:
Install the Redis Driver on Cloud9
nada-nix install redis
Install Redis library for NodeJS
npm install redis
Thanks for anyone who was trying to help me.
You can run the redis-server using your own config file.You can create your own config like below.
//port and ip of ur redis server
port 6371
bind 127.0.0.1
//password for this server
requirepass ucanmentionurpwd
//storing snapshots of the data
save 60 1
dbfilename dump.rdb
dir /tmp/db
//starting redis server
redis-server //ur config file location
See this link for redis configuration
https://raw.github.com/antirez/redis/2.6/redis.conf
If you mention requirepass with your password means only you need to do
client.auth('urPwd');
Otherwise no need to call the client.auth method.

Node.app - runs locally, deploys correctly but fails to work

i'm trying to deploy my node app on heroku.
This app works fine localy, and is properly pushed on heroku.
However, whenever I try to reach the URL, the browsers displays:
An error occurred in the application and your page could not be served. Please try again in a few moments.
If you are the application owner, check your logs for details
At first, I thought that was because I didn't pay attention the port I was listening to (I don't really understand how Heroku processes and if the port is important) so I copied the same code as they did in their Getting started...:
port = process.env.PORT || 5000;
server.listen(port, function() {
console.log("Listening on " + port);
});
my package.json is very basic :
{
"name":"Radiorev",
"version":"0.0.1",
"dependencies":{
"express":"3.3.5",
"socket.io":"0.9.16"
}
}
Help!
By the way when I type heroku log, my cmd tools displays something quite unreadable... with zero line breaks.
1
Heroku does not support websockets, so try turning it off
// assuming io is the Socket.IO server object
io.configure(function () {
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
});
as per Using Socket.IO with Node.js on Heroku
2
update: H14 - is Error H14 (No web processes running)
try adding Procfile with following contents:
web: node server.js
The problem might be that you scaled your web dynos to 0, so there's no one to run the server. Try running:
heroku ps:scale web=1
(Reference: https://devcenter.heroku.com/articles/error-codes#h14-no-web-dynos-running)

Resources