Getting an error Socket is trying to reconnect to Sails - javascript

My Sails version is 0.11.2 and running with port 1337
In assets/js/dependencies/sails.io.js, i can see version as 0.11.0
Below is the client side script.
<script src="http://localhost/project/assets/js/dependencies/sails.io.js"></script>
<script type="text/javascript">
// `io` is available as a global.
// `io.socket` will connect automatically, but at this point in the DOM, it is not ready yet
// (think of $(document).ready() from jQuery)
//
// Fortunately, this library provides an abstraction to avoid this issue.
// Requests you make before `io` is ready will be queued and replayed automatically when the socket connects.
// To disable this behavior or configure other things, you can set properties on `io.sails`.
// You have one cycle of the event loop to set `io.sails.autoConnect` to false before the auto-connection
// behavior starts.
io.socket.get('/hello', function serverResponded (body, JWR) {
// JWR ==> "JSON WebSocket Response"
console.log('Sails responded with: ', body);
console.log('with headers: ', JWR.headers);
console.log('and with status code: ', JWR.statusCode);
// first argument `body` === `JWR.body`
// (just for convenience, and to maintain familiar usage, a la `JQuery.get()`)
});
I am getting the error like
Socket is trying to reconnect to Sails...
When i checked some other posts, there saying something related with sails version.
I tried to change the sails.io.js version to 0.11.2, but still same error.
Is this error have any connection with port ?
Because the response from below request is 404
http://localhost/socket.io/?__sails_io_sdk_version=0.11.2&__sails_io_sdk_platform=browser&__sails_io_sdk_language=javascript&EIO=3&transport=polling&t=1444654910110-52
Response
<p>The requested URL /socket.io/ was not found on this server.</p>
<address>Apache/2.2.22 (Ubuntu) Server at localhost Port 80</address>
Any help what is wrong ?

You're running the Sails app on port 1337, but loading the sails.io.js file from port 80 (because you don't specify another port):
<script src="http://localhost/project/assets/js/dependencies/sails.io.js">
I guess you have an Apache server running on port 80, so it's finding the sails.io.js file and returning it, but then the socket client assumes that it should be connecting on port 80 as well.
Either update your script tag with a port:
<script src="http://localhost:1337/js/dependencies/sails.io.js">
or specify an alternate URL for the socket to connect to, using the following code before the io.socket.get:
io.sails.url = "http://localhost:1337";

The 404 is coming from a client-side socket attempting to connect to the server, which is not accepting socket connections.
if you are not using a socket in your application you need to delete the sails.io.js script.
and below both options didn't work for me but this worked. I deleted the sails.io.js file.

Related

Nodejs Socket.io xhr poll error, curl error, file_get_content error

i need help. I create a nodejs server when i am on cloudflare. I quit cloudflare and now. I cannot send request socket.io xhr (return poll error in cache), curl call (return null) or file_get_content (return false) etc to the node server. I run the server on port 8443. I already unlock the port in the firewall. When i open in my browser, it's work! but...
SSL certificates is on and opérationnal. I want to send/get data to socket server from mon PHP file.
app.get("/receiveMDNDWEYS", function(request, response) {
console.log("receive");
console.log(request.query); //This prints the JSON document received (if it is a JSON document)
if(request.query.password && request.query.password == '****'){
request.query.mp = encode(request.query.mp, {
'useNamedReferences': true
});
response.status(200).send('ok IS SEND');
} else {
response.status(400).send('error');
}
});
My server Almalinux 8 with directadmin.
disable firewall and still not working
add port at the same power of 80,443 still not working
android app, delete cache work one time xhr poll error next app open
i don`t really understand why now that's not working and before yes! Before i have cloudflare activate and now i use my own DNS system. I miss something ?

NodeJS: Send HTTPS request but get HTTP

I am building a website using NodeJS, and I deploy it to Heroku. But when I open the website, something went wrong. Here is the problem:
Code:
In the main source file of my web:
app.get('/', (req, res) => {
var data = {
rootURL: `${req.protocol}://${req.get('Host')}`,
};
res.render('home.html', data);
});
Then, in home.html, I include the following script:
<script type="text/javascript">
$.getJSON('{{rootURL}}'+'/about', {}, function(data){
// Code here is deleted for now.
}).fail(function(evt) {
// Code here is deleted for now.
});
</script>
Here I use hbs template, so {{rootURL}} is equal to the 'rootURL' property within the 'data' object rendered along with the 'home.html' page.
The '/about' is one of the APIs I designed for my web. It basically sends back something about the website itself and this information is wrapped in JSON.
Then, here comes the problem. The code works fine locally, and works well when I send HTTP request instead of HTTPS to Heroku. But if I send HTTPS request to Heroku, I'll get 'Mixed Content' Errors:
Errors I get in Chrome Console.
I then switched to 'Elements' tab in the developers tool, and I saw this:
The schema is HTTP, not HTTPS!
I'm very confused here. I just grab the 'protocol' property within the 'req' object, and fill in the template with it. So, I'm assuming if I enter '[my-website-name].herokuapp.com' with 'https' schema in my Chrome Browser, my nodeJS app deployed on Heroku should get 'https' for req.protocol. But Apparently it's not the case. What is wrong here?
I assume you don't actually have an SSL certificate? Heroku will be providing the HTTPS, but it will then translate it to normal HTTP internally when it hits your express endpoint, which is why it sees req.protocol as HTTP.
Is there any point in even providing the URL to getJSON? Why not just send it $.getJSON('/about', callback) and let the browser handle that?
Also, you haven't hidden your URL in that first image you uploaded, if that's what you were intending.
Heroku router is doing SSL termination, so no matter if you connect via http or https, you get http on your side. Original protocol is however set in X-Forward-Proto header. You need use this value.

Websocket Server, based on socketo.me not accepting connections from shared internet

I managed to configure a simple websocket server according to this tutorial in AWS EC2 instance and its working fine.
But only from my home internet connection which has a real IP and told as a dedicated internet line.
I tried with a very simple javascript example code from client side (using a HTML page) and it works perfectly if I use that dedicated internet connection from my PC/Mac. (I used autobahn.min.js) above the following script.
var conn = new ab.Session('ws://X.X.X.X:8080',
function() {
console.log("Connection established!");
// To Do: Subscribe with client ID
},
function() {
console.warn('Connection closed!');
},
{'skipSubprotocolCheck': false}
);
but it fails if I run the same simple file/script from under another shared internet connection such as cellular data or something like that. I get the following error in browser console.
WebSocket connection to 'ws://X.X.X.X:8080/ws' failed: Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT example.com Connection closed!
The server is in AWS EC2 instance. Yes, 8080 is enabled under security group. Actually all works fine except client connection goes from some specific types of internet connection based computer.
Thanks in advance for any help!

event stream request not firing close event under passenger apache

So I have an event stream in my express js node application. Here's an overview:
app.get('/eventstream', function(req, res){
req.socket.setTimeout(Infinity);
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
});
res.write('/n');
req.on('close', function(){
console.log('connection closed');
});
}
On my local dev box, running from the command line with
node app.js
it works fine, and prints out 'connection closed' when i close my tab in my browser.
However, when running on my server, under Apache with Passenger, it doesn't print out any message - the server seems to not fire the 'close' event. I'm using this event to remove from my count of active users. Any ideas?
Cheers,
Dan
Phusion Passenger author here. The short answer is: technically, the connection hasn't closed yet.
Here's the long answer. If the client connects directly to your Node.js process, then yes, the connection is closed. But with Phusion Passenger there's a proxy in between the client and Node.js. The thing about sockets is that there are two ways to find out whether a socket has been closed: either 1) by reading end-of-file from it, or 2) by writing to it and getting an error. Phusion Passenger stops reading from the client as soon as it has determined that the request body has ended. And in case of GET requests, that is immediately after header. Thus, the only way Phusion Passenger can notice that the client has closed to the connection, is by sending data to it. But your application never sends any data after that newline, and so Phusion Passenger doesn't do that either and never notices that the connection is closed.
This issue is not limited to Phusion Passenger. If you put your Node.js app behind a load balancer or any other kind of reverse proxy, then you could also run into the same issue.
The standard solution is to regularly send "ping" messages, with the purpose of checking whether the connection is alive.
A similar issue also applies to WebSockets. It is the reason why the WebSocket supports ping frames.
UPDATE February 28, 2016:
We have found a solution, and Passenger 5.0.26 and later supports forwarding half-close events which fixes the problem described by #coffeedougnuts. Just use 5.0.26 and later and it'll work as expected.

Chrome websockets, readystate always on 0

I'm trying to use websockets on a website, so first I developed a small and very simple websocket page just to test it.
I have a websocket server running on my localhost, it is based on the python Tornado "Chat" demo. For some reason the chat demo app runs perfectly but I can't seem to use the websocket with my own page although some form of connection is made.
I am testing this using the latest Chromium version, so implementing websockets version 13, which is supported by the Tornado implementation.
So here is the problem:
Load page, js executes and Upgrade request is sent to the server
Server receives request and answers
So here in my understanding Chrome should set readyState = 1 and I should be able to send messages from my page.
Only for some reason it doesn't work, readyState remains 0 and of course if I try to send a message I receive an INVALID_STATE_ERR.
Here are the Headers :
Request:
GET ws://127.0.0.1:8000/chatsocket HTTP/1.1
Origin: http://127.0.0.1
Cookie: _xsrf=9f73731fc2d544df864ce777bef0775a
Connection: Upgrade
Host: 127.0.0.1:8000
Sec-WebSocket-Key: pkwlpY+TtxfgUrm3M4WtTQ==
Upgrade: websocket
Sec-WebSocket-Version: 13
Response:
HTTP/1.1 101 Switching Protocols
Connection: Upgrade
Upgrade: websocket
Sec-WebSocket-Accept: ur9KL2jBhYB38e2SgwOkjyBlQXk=
Any help is appreciated :)
--- EDIT ---
So I figured it out in the end, if you run into the same problem here is the reason:
WebSocket's readyState is updated when the Thread ENDS !
So running a code like:
var ws = new WebSocket(stuff);
while(ws.readyState==0){};
Will send the browser in an infinite loop...
Running code like:
var ws=new WebSocket(stuff);
do_other_stuf();
Might work, but you wont be able to use WS.
If the code that is supposed to run after the socket opens uses the socket this is the way it will have to be written:
var ws=new WebSocket(stuff);
ws.onopen = new function(){
// some code that need WS to be open
}
// rest of the code that doesn't require WS to be open
A better way to do this would be to let the thread end by using an asynchronous call:
var ws = new WebSocket(stuff);
setTimeout(whatever,500);
function whatever(){
if(ws.readyState==1){
// The code you want to run after WS is open
}
else
setTimeout(whatever,500);
}
In order to trigger some functionality when WebSockets are connected please use callbacks:
var socket = new WebSocket(...);
socket.onopen = function() {
// socket is connected
};
socket.onerror = function() {
// some error happened
};
socket.onmessage = function(evt) {
// you get a message: evt.data
};
Using readyState is well bad thing to do in such case, especially if you have to ask it multiple times (is just unnecessary).
Additionally take in account that each vendor of browsers implements WebSockets with very slight differences (unfortunately), so make sure you test it in most browsers.

Categories

Resources