PhantomJS doesn't start while websocket connection is open - javascript

I'm working on slack bot and I've encountered curious problem. I have a module which scrapes web-page using phantomJS (via SpookyJS & CasperJS on top of it). I wrote this module and tested it running it from command line manually. It works well. But then I added slackbots npm module which abstracts Slack realtime API, and created a module with bot class. This bot module requires my module with scraping code (phantomJS) and calls its function when message event triggers:
var getAnswer = require('./getAnswer');
myBot.prototype._onMessage = function (message) {
if (this._isChatMessage(message) &&
this._isChannelConversation(message) &&
this._isMentioningMe(message)) {
this._reply(message);
}
};
this._reply basically just calls getAnswer(originalMessage.text) and then self.postMessageToChannel(channel.name, reply, {as_user: true});
getAnswer returns a promise, but it never get's fulfilled. I made CasperJS be verbose and saw that nothing happens after
[info] [phantom] Starting...
Everything just hangs...
I have no idea, how to fix this. I guess it's because slackbots module establishes websocket connection when I call Bot.prototype.run. Any suggestions?

As I said I use Spooky to spawn child CasperJS process. I went to Spooky documentation page and read this:
Specifically, each Spooky instance spawns a child Casper process that
runs a bootstrap script. The bootstrap script sets up a JSON-RPC
server that listens for commands from the parent Spooky instance over
a transport (either HTTP or stdio). The script also sets up a JSON-RPC
client that sends events to the parent Spooky instance via stdout.
I used http as a transport and it didn't work, so I changed it to stdio and it helped. I'd appreciate it if someone could explain why it helped?

Related

React Native -- cannot start because HTTP server already being used by another WebSocket server

I'm running npx react-native start/npx react-native run-android, and everything builds fine; however, the app cannot start a server properly. This is the error I'm getting:
Error: The HTTP/S server is already being used by another WebSocket server
at new WebSocketServer (.../node_modules/ws/lib/websocket-server.js:83:15)
at Object.attachToServer (.../node_modules/react-native/node_modules/#react-native-community/cli/build/commands/server/messageSocket.js:93:15)
at Object.runServer [as func] (.../node_modules/react-native/node_modules/#react-native-community/cli/build/commands/server/runServer.js:127:37)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at async Command.handleAction (.../#react-native-community/cli/build/index.js:164:9)
I've tried killing all react-native related processes, and even if I reboot the system, this error still persists. Any ideas on what might be causing this?
It looks to me like your processTicksAndRejections() method is trying to set up its own WebSocketServer instance to do its work, rather than using one that's already set up. Your error message means that you
already have an http/s server going
already have a websocket server set up on it
you're trying to set up another websocket server on that existing http/s server.
If you were trying to run two server processes on the same port, a different error message would show up.
I ended up solving this problem by removing the dependency #react-native-community/cli-platform-android altogether, but I'm not completely satisfied with the solution because I have to run npx react-native start and npx react-native run-android separately.

Pointing socket.io client to heroku service

I have used socket.io, Node.JS, and express to create a real-time chat application that I could view by creating a local server. However, instead of using a local server, I would like to point my client to an existent heroku service. How do I go about doing this?
When I do the following,
var spot = io("https://spotim-demo-chat-server.herokuapp.com");
all functions relating to connecting, disconnecting, username registration, and messaging seem to fail. They do not function in the chat nor do their console.log messages appear in terminal.
I have also considered creating a HTTP server and having it listen to the port and IP address of the heroku service with .listen(). However for my assignment, I was not provided with either of these values, simply the heroku url.
I'm not sure where you're having trouble without more code from you. I've setup a basic socket.io client as follows and can connect without issue. You can run it with the following to see just the client's messages DEBUG=spot-client node client.js or if you wish to see all the debug info from the socket client itself as well DEBUG=* node client.js. The latter might help you diagnose the issue further. Also be sure to install the socket.io-client and debug packages. Hope this helps!
const io = require('socket.io-client');
const debug = require('debug')('spot-client');
var spot = io("https://spotim-demo-chat-server.herokuapp.com");
spot.on('connect', function(){
debug("connected");
});
spot.on('event', function(data){
debug("event",data);
});
spot.on('disconnect', function(){
debug("disconnect");
});

Stop a process by port from javascript

I'm using jest to write some tests for a node.js application. I have a server server.js and a test file server.test.js. In my server.js I use the line
var secureServer = https.createServer(options,
app).listen(config.node_port, () => {
logger.info("Service running on " + config.node_port)
});
to start my server on port 8082. In server.test.js I use
var posClient = require('./pos-ClientJS.js');
to get access to the functions, that I have to test. When I run my test I get the following console output:
Jest did not exit one second after the test run has completed.
This usually means that there are asynchronous operations that weren't
stopped in your tests. Consider running Jest with `--
detectOpenHandles` to troubleshoot this issue.
So my question is: Is there a way to stop the server running with javascript code like stop(8082) or sth.? Or is there a less difficult way to solve this problem without stopping the process?
From the Nodejs HTTP module documentation, you can call secureServer.close() to stop listening at the specified port. If the module exposes the server to Jest, you can use the teardown methods to stop the server automatically after tests complete.

MongoDB Cloud9 Connection

So, I am wondering if there is a way to connect to the mongoDB I have setup in my Cloud9 from an html. I mean, I have already connected to the db from the terminal and everything is working like a charm but I need to do some stuff inside my script in an html document and when I try calling the function which contains this code it does nothing
var MongoClient = require('mongodb').MongoClient
, format = require('util').format;
MongoClient.connect('mongodb://127.0.0.1:27017/ingesoft', function (err, db) {
if (err) {
throw err;
} else {
console.log("successfully connected to the database");
}
db.close();
});
I have saved the same code into a "file.js" and ran it from console using node file.js and it outputs into the console log "successfully connected to the database", plus the terminal which is running mongo's connection shows me one more connection to the db. The thing is, when I try to run that code inside my script it doesn't work. Sorry for my ignorance I am new to mongo.
Any help would be much appreciated
To simplify your question, here's what's going on:
node file.js containing the code in your question is working
pasting the same code to your html file is not
So, getting to the bottom of the issue, let's ask first: what's the difference between running node file.js and putting the code in html?
The difference is that node ... is running on your Cloud9 workspace (let's call it the server machine).
Your MongoDB server is also running on that server machine
The mongodb npm package you installed is also present on the server machine
The url: mongodb://127.0.0.1:27017/ingesoft references 127.0.0.1 which is the localhost for your server
whereas with the code on your browser:
The code is being run on your customer's machine
That machine doesn't have your Mongodb server
Browser's usually don't support require
You can do requires if you bundle code and use something like webpack or browserify. Did you perhaps do that?
If you did indeed package everything, was the mongodb package that you're requiring packaged?
Can the mongodb package be run from the client side?
The url: mongodb://127.0.0.1:27017/ingesoft references 127.0.0.1 which is the localhost for your customer's machine
Basically, as you can see from the above, the two are very different.
If you want to talk to your db, a lot of people go the following route:
Make a server application that implements some form of REST API
That REST API talks to your DB
Your client code knows how to talk to the REST API and get the required data
That way, you only talk to your MongoDB using your server, and the client can talk to your server via the internet.
This is, of course, an oversimplification, but I hope this resolves your confusion.

Node.js - Server crashes when started on startup, doesn't crash when manually started

I am running Node.js and Socket.io for online chat.
I have created a file in:
/etc/init/example.conf
it has two lines:
start on startup
exec forever start /var/www/example.com/html/server.js //location of server file.
Whenever I start file upload in chat application, it crashes but instantly restarts.
Whenever I kill node process though, and start it manually - it works fine.
I also can't get any logs or anything from terminal as when it's auto started - it doesn't print me anything to terminal.
I am still new to Node.js and Linux in general.
Node.js is running on Express + Jade.
How do I determine specific cause?
I managed to solve my problem, after a bit of searching around I found out about tail command.
My issue was a bit harder to trace because Node.js was a process started by autostart so when I launched terminal and connected to server, process was just running in background basically and I wouldn't get any output (including exception messages).
Anyway, solution that worked for me was:
I typed
ps aux | grep node //to find PID of node process
then I went to following directory
cd /proc/[pid of running node service]/fd
In fd directory there are few objects you can get output from but if you want to attach and listen to servers output including uncaught exceptions, you need 1.
So:
tail -f 1
that way I was able to cause website to crash and see the output.

Categories

Resources