I am using socket.io on heroku, and on socket connection I would like to get the client ip,currently I am getting an IP which is not the real IP (I think this is heroku's ip).
I am using socket.manager.handshaken[socket.id].address.address to get the user's ip.. do you have another way?
Try:
socket.manager.handshaken[socket.id].remoteAddress
If you need the client port then that would be:
socket.manager.handshaken[socket.id].remotePort
Related
My application is running on ec2 instance. we are using node.js for server side code. We are using socket.io, express to connect the client side code.I have a requirement to capture user's browser ip and send it to server side code.
i have tried the below code but it is giving me the server IP details.
io.sockets.on('connection', function (socket) {
var socketId = socket.id;
var clientIp = socket.handshake.headers;
console.log('connection :', socket.request.connection._peername);
console.log(socket.request.client._peername.address);
console.log(clientIp);
});
Is there any ways to capture the user's browser IP, it will be great help.
I appreciate your suggestions.
In your client side code, you cannot tell what IP address you will be connecting from.
On the server side (express server), you can easily grab the remote IP address from the request, like so:
console.log(req.connection.remoteAddress);
Note that just like any other server, this only tells where you see the connection coming from - the user might be using a VPN, or behind a corporate firewall, etc., in which case the IP address may not have much meaning. Whether this matters to you depends on why you are trying to collect it (logging or something more meaningful).
Don't forget that if your express app is behind a web server (like nginx), you may need to look at the forwarded-for header instead - see related S.O. question How to get remote client address.
we are not yet using nginx. we are just running with nohup command in the background to up the server in ec2. I tried using the below command
console.log(request.socket.remoteAddress)
it gives me the server side ip not the client side ip. We are using this app :8000/index.html in our local system. i want to capture the local system ip. This console.log(req.connection.remoteAddress) gives me TypeError: Cannot read property 'remoteAddress' of undefined
I am new to Heroku and I am trying to deploy NodeJS application that is using socket.io to listen.
I am able to deploy that to local and its working fine. but when running the same in Heroku it is not responding.
Is there any specific port that we need to assign for socket io in Heroku?
I can't use process.env.PORT as I am using that for app server port.
Any Idea to solve this issue? or do we need to enable some config to use socket io?
I think we need 2 ports, one for normal express server and other for socket io. How to get the 2nd one.
There is no way to open more ports than 80 to the public. But WebSockets and especially socket.io should work on heroku.
Have you read these articles:
https://devcenter.heroku.com/articles/node-websockets
socket.io -> https://devcenter.heroku.com/articles/node-websockets#option-2-socket-io
http://robdodson.me/deploying-your-first-node-dot-js-and-socket-dot-io-app-to-heroku/
It's not a problem to have the websockets on port 80 because the protocol is ws:// (wss://) and it works with http:// (https://) side by side.
How can I put the server IP address when I connect to the server in the frontend?
var socket = io.connect('http://localhost:3000/panel');
So instead localhost put the correspondent server IP address for the production environment.
Thanks.
Since this Javascript is executing in the client, you can just do the following:
document.location.hostname contains the hostname, and document.location.port the port. It's as easy as that.
As my app is currently under development, my local computer is temporarily acting as the server. Using the service by no-ip.com, I have managed to establish internet connection to the NodeJS server at my home, which has been supported by socket.io. However, although the HTTP connection is fine, every now and then the socket.io connection would fail until I restart the server. I have been investigating the cause of this. I wonder whether when the dynamic IP of the server changes, the socket.io which is listening to the ports fails. Could someone confirm this with me?
Its definitely not the answer of your question but i think you're gonna like it ! use NGROK download & install it
once it's done
launch your dev server
open cmd and type
c:\>ngrok http 3000
3000 is your dev server PORT so if its something else change it
This will give you an address like that https://xxx232xx.ngrok.io
use this address to access your app now from any connected device
One last thing , when you use socket.io with https domains change your config and add the address
Example
var ioSocket = io('https://xxx232xx.ngrok.io', {
'reconnection delay': 2500,
'secure':true,
'max reconnection attempts': 10,
'reconnection':true
});
Hope it helps !
My little node.js server works fine, but only the host machine is able to connect with it. When i try to connect with other clients (which are connected to the same router as the host), I don't get a response. The server is listening at port 3000 (also tried 80, no difference), I tried setting the host ip to the network ip of the host computer, 127.0.0.1 and left it empty, but nothing worked.
How do i configure my server so that clients other than the host may connect?
Set the listen IP to 0.0.0.0. This listens on every interface.
Turn off your firewall to check that it is not the issue. When you turn it back on, allow port 3000 TCP incoming and outgoing.
127.0.0.1 is your local IP
if you are using wamp or mamp share your server on ip.
Next go on google check for whatsmyip. Give your ip adress to who you want to share your file.
Or if you consider to upload on server
except node_modules you have to upload the necessary files and folders. In server try npm install.
Make sure you have package.json
If you are new to Nodejs. Try Heroku
https://scotch.io/tutorials/how-to-deploy-a-node-js-app-to-heroku