socket.io on openshift with angular-fullstack - node.js

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.

Related

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.

How to write and test dynamic server for Office.js add-in?

I'm developing an Office.js add-in for Excel, and I'm kind of lost on how to create the server side of the application and test it on localhost.
I've created the add-in project/structure using Yo Generator, and I'm using gulp to test it on localhost (port:8443). Using this approach, I was able to successfully load my add-in and test the client-side of the same. Also, I've tested a http request to a static json file and it worked fine.
The issue is that I need to run code on server side for dealing with files and doing some processing, and I simply can't find a way to do that.
I've already tried to start a localhost server on a different port (port:8000) using the code bellow and node command:
var https = require('https');
var fs = require('fs');
var httpsOptions = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
};
var app = function (req, res) {
res.writeHead(200);
res.end("hello world\n");
}
https.createServer(httpsOptions, app).listen(8000);
The server started ok, but as my application is running on port:8443, I'm unable to do cross origin requests (which I understand would also not work on a production environment).
I also tried to start add-in server on port:8443 using gulp serve-static command, and then start a server listening on the same port:8443 using node command, but this results on the error bellow:
Error: listen EADDRINUSE 127.0.0.1:8443
at Object.exports._errnoException (util.js:870:11)
at exports._exceptionWithHostPort (util.js:893:20)
at Server._listen2 (net.js:1234:14)
at listen (net.js:1270:10)
at net.js:1379:9
at GetAddrInfoReqWrap.asyncCallback [as callback] (dns.js:64:16)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:83:10)
May someone please help on how to get this working?
Please let me know if any further information is required.
Thanks in advance.
First, you can't have two servers listening on the same port simulateneously. That's why you're getting the Address in Use error.
Second, I am in a similar situation like you and my thinking here was, that the way to go would be writing a server that provides an API (e.g. REST). Then, the javascript code that get's loaded into office (for starters the App.js in your yo office generated project) makes requests to this API.

Parse server error "Protocol not supported"

I've been migrating datas for two days now, everything is ok in AWS - I used a Bitnami MEAN machine, it was only a very small app.
FYI, I'm moving from Heroku + Parse, set up also nginx on AWS to run more than one nodejs app.
I had to downgrade the default mongodb installation due to incompatibility with Parse (WHY?)
So, straight to the problem: installed node.js parse server, configured like they show on git
var api = new ParseServer({
databaseURI: 'mongodb://127.0.0.1:27017/database',
cloud: './cloud/main.js',
appId: 'my-app-id',
masterKey: 'my-master-key'
});
but when I try to execute any query I got
Error: Protocol not supported.
at send (/opt/bitnami/apps/bellboy-admin/node_modules/xmlhttprequest/lib/XMLHttpRequest.js:299:15)
at dispatch (/opt/bitnami/apps/bellboy-admin/node_modules/parse-server/node_modules/parse/lib/node/RESTController.js:137:11)
at Object.ajax (/opt/bitnami/apps/bellboy-admin/node_modules/parse-server/node_modules/parse/lib/node/RESTController.js:139:5)
at ParsePromise.<anonymous> (/opt/bitnami/apps/bellboy-admin/node_modules/parse-server/node_modules/parse/lib/node/RESTController.js:208:29)
at ParsePromise.wrappedResolvedCallback (/opt/bitnami/apps/bellboy-admin/node_modules/parse-server/node_modules/parse/lib/node/ParsePromise.js:135:41)
at /opt/bitnami/apps/bellboy-admin/node_modules/parse-server/node_modules/parse/lib/node/ParsePromise.js:196:35
at runLater (/opt/bitnami/apps/bellboy-admin/node_modules/parse-server/node_modules/parse/lib/node/ParsePromise.js:180:12)
at ParsePromise.then (/opt/bitnami/apps/bellboy-admin/node_modules/parse-server/node_modules/parse/lib/node/ParsePromise.js:195:9)
at Object.request (/opt/bitnami/apps/bellboy-admin/node_modules/parse-server/node_modules/parse/lib/node/RESTController.js:201:8)
at Object.find (/opt/bitnami/apps/bellboy-admin/node_modules/parse-server/node_modules/parse/lib/node/ParseQuery.js:1141:27)
I tried almost everything, any ideas from you?
Did you install the dependencies for ParseServer? More specifically, is the MondoDB NodeJS drive installed?
npm install mongodb
If it helps, I have a tutorial that explains how the ParseServer should be setup, providing you have MongoDB and NodeJS already installed to the correct versions.
Solved
I guessed it was something involving the http/https protocols between my node app and parse server so I just added the http:// before the address of Parse.serverURL
Parse.initialize('my-id','unused');
Parse.serverURL = 'http://localhost:3030/parse';
Maybe it goes by default on https when not specified.

Heroku Node.js WebSocket Server

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?

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.

Resources