socket.io referring to localhost despite being set as a specfic IP - javascript

I'm trying to use web sockets to connect from a Google Chrome browser on my phone to a server running node.js and socket.io.
Using the remote debugging tool in Google Chrome I get this error in the console
Failed to load resource http://localhost/socket.io/1/?t=1368744562638
This happens despite me specifying my internal LAN IP in code for the client like so:
var socket = io.connect('http://192.168.1.3');
Furthermore it seems like the first heartbeat request makes it but starts to fail after that.
The code runs as expected when running the client on the server.

I am of course a idiot. I had another javascript file that had not been updated to connect to the specific IP I had set and was still set to "localhost".
After updating the host that socket.io should connect to in that javascript file everything is now running smoothly :)

Related

Tell Javascript code whether the App is running in remote host or localhost

I have a host name in javascript function and use Ajax to send JSON to server, I test it in localhost and I upload to remote host to test again.
Everytime I upload my web app to remote host and I need to manually change the host name to remote host name to test it,
localhost => mydomain.com
I have to change it back if I test it in my local machine.
mydomain.com => localhost
BTW, the font end is Javascript Html CSS, The backend is not Node.js and Not javascript. (It is Haskell)
I'm wondering how to tell my javascript code I'm running my App. in remote host or I'm running my App. in localhost so that my javascript can use different hostname in the code.
My current solution is I get the OS name in the backend code, and create
<div id='myid'>OS_Name</div>
and hide in my html page, and my javascript function read the 'id=myid' to get the OS_Name so my javascript code can distinct localhost or remote host.
I'm sure there better solution for this problem.
Can anyone have any suggestions?
You can use process.env.PUBLIC_URL to get the base url of where you app is running in node. So instead of constantly changing localhost to mydomain.com and back again, you can use process.env.PUBLIC_URL.

Can't access browser Notification

I want to setup push notification on a web app, but I want to test it first on my local dev server (which is set with vagrant) so my local server looks something like htttp://mylocalserver.com. (I want to point out that it's really hard to change it to 127.0.0.1 because this vagrant box was made by someone else and there are also other stuff that makes it complicated to change it)
The first issue I encountered was
"Service Worker Error DOMException: Only secure origins are allowed "
which i solved using the solution in "unsafely-treat-insecure-origin-as-secure" flag is not working on Chrome
But then I realized i can't use any of the Notification methods like: Notification.permission or Notification.requestPermission.
When I try to get the Notification.permission it returns "undefined". Is there a way i can make this functionality work?
You can't use any of the fun stuff without serving it over https, with the exception of 127.0.0.1
You could try using the chrome web server extension which will run a test server on your local machine, or set up a restricted access area on a public server secured with https.
You could also try and generate self signed certificates for your local server, but I'm not sure if chrome will play ball with those.
I found the problem in my case. The application had a model with the same name "Notification" on the window object which overrode the browser Notification.
And of course my "Notification" didn't have those methods (Notification.permission, Notification.requestPermission() ... etc) so I changed the name of the model and everything worked fine.

How to get node.js/socket.IO server work with cloudflare?

I have a node.js/socket.IO server that has been under attack recently, so I decided to start using cloudflare to hide my server IP. However, anyone can easily get the server IP from the client javascript file. Is there anything I can do to connect through cloudflare and not my server directly, so I can help prevent attackers from getting the server IP?
E.g.:
var client = io.connect('http://141.101.xxx.xxx:466');
That would be the IP when I ping the domain that is using cloudflare. I try to connect to it directly.
Even attempting to connect to the domain itself doesn't seem to work
var client = io.connect('http://mydomainthatusescloudflare.com:466');
Only thing that works is directly connecting to the server, without going through cloudflare (thus revealing the IP)
var client = io.connect('http://217.xxx.xxx.xxx:466');
You can use cfdomain:80 for socket.io and add this line,
io.set("transports", ["xhr-polling", "jsonp-polling"]);
but disconnected event isnt work. I am trying to fix it.

nodeJS chat app created. How to run

I have created nodeJS io.socket chat app using the link here -
http://cestfait.ch/content/chat-webapp-nodejs
I am able to run it on my laptop. I did not find any help on google how to connect to nodeJS app from other machines. I have another laptop which I have connected through wifi.
Now I really want to know how can I connect another user from another laptop to the nodeJS server running on my first laptop.
I will be very grateful for your help. Any help in broader sense will also be ok.
EDIT 1:
When I am trying Brandon's method, I am getting following error. Not sure what is wrong. I have to run the file client.html directly in the browser. Running http://192.168.43.244:8000/client.html throws the error.
C:\node>node server.js
info - socket.io started
C:\node\node_modules\socket.io\lib\manager.js:0
(function (exports, require, module, __filename, __dirname) { /*!
^
RangeError: Maximum call stack size exceeded
It's complicated.
If you don't have a static IP address (and you probably don't) that means every time you reconnect your computer to the internet, you are going to get a new IP address, so you'd have to update your code every time.
Also, there are issues with Firewalls, both on your dev machine and with (wifi) routers.
Best solution is to acquire a static IP. But its not cheap
First, make sure Socket.IO is connecting to the server serving the HTML. In client.html, change:
var socket = io.connect('http://localhost:8000');
to
var socket = io.connect();
Leaving out the host/port will cause Socket.IO to connect to the same host and port serving the HTML.
Once you've done that, you can access the server from another computer by visiting http://1.2.3.4:8000, where 1.2.3.4 is the IP address of the computer running the server. The connecting computer and the serving computer will need to be on the same network for this to work; if you want to (temporarily) connect with someone on a different network, you can use a tool like localtunnel.

Chrome & Safari revert to xhr-polling rather than websockets with socket.io

I have a problem with websockets and socket.io. When I try to connect to my node server with socket.io it initially connects using websockets but when reverts to jsonp-polling shortly after.
This is the output from the node sever when I connect:
8 Jun 07:01:15 - Initializing client with transport "websocket"
8 Jun 07:01:19 - Initializing client with transport "jsonp-polling"
8 Jun 07:01:19 - Client 16630339180119336 connected
This happens in Chrome & Safari.
I have updated to the latest socket.io version 0.6.17 and am running node 0.4.7.
I have tried deleteing my cookies and cache as suggested on github and SO, however the problem remains. Also, when I try to force websockets it never fully connects with a session ID.
Does anyone have any ideas?
Websocket API is not supported by default in all the browsers at the moment (as per my knowledge) it should work on chromium though try testing it on chromium or firefox(after editing the default settings)and see if that still reverts to XHRPolling.
I am running it on a different IP as I need to run node on port 80 which causes conflict on my web server with Apache. Can websockets/flashsockets not be use cross-domain?
Now there might be 2 different reasons for the bug from here
Web/Flash Sockets will not let u connect to the node.js client unless either u specify a differnt port like 81 or u specially specify apache to proxy the incoming request to Node.
an easy solution could be writing the Node.js based HTTP server to just relay data from Apache (and setting Apache to run on a differnt port then 80)
This link tells how to do that... in this process you can make Node.js do something like check if the request is from a websocket/httpbrowser if thats an http browser forward the request to Apache if not ie if thats from web/flash sockets then handle the socket accordingly. or as commented on the question. Specify APACHE to proxy to Node.js.
Flashsockets require you to serve a crossdomain policy file on port 843 are you sure you are providing a cross domain file? (I think socket.io has inbuilt functionality to do that but still its always good to check.)
As told on the socket.io main website
In order to provide realtime connectivity on every browser, Socket.IO selects the most capable transport at runtime, without it affecting the API.
WebSocket
Adobe® Flash® Socket
AJAX long polling
AJAX multipart streaming
Forever Iframe
JSONP Polling
It's pretty clear that it will revert to AJAX Long Polling if websockets are disabled and Adobe Flash Socket fails to connect (this might be due to the unavailability of the policy file).
Here's a sample code for the cross domain file which you can include in your code and see if that makes your server run with websockets.
var net = require("net");
// Node.js
var Policy = net.createServer(function(socket)
{
socket.setEncoding('utf8');
socket.on('connect',function(){
console.log("Policy Request");
socket.end("<?xml version=\"1.0\"?><!DOCTYPE cross-domain-policy SYSTEM \"/xml/dtds/cross-domain-policy.dtd\"><cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"*\" secure=\"false\"/></cross-domain-policy>");
});
});
Policy.listen(843);

Categories