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.
Related
I have a Server socket and a device which uses TCP long-connection mode.
They can connect and exchange data together. The node.js server is something like this:
net.createServer(function (socket) {
console.log('ip:port' + socket.remoteAddress +':'+ socket.remotePort);
socket.on('data', console.log);
}).listen(‘0.0.0.0’, 8888);
The device connects just right and I'm able to receive data from it.
I can send commands to it by using the same process, by just doing socket.write('dothisplease') and this works too.
Now I have another worker process which should be sending commands at regular intervals. I can get ip and port from console.log when the device connects, it looks like: xx.xxx.xx.xxx:63024
I tried using this combination ip:port to create new connection:
var client = new net.Socket();
client.connect(device_port, device_ip, function () {
client.write('dothisplease');
});
... but the result was ECONNREFUSED
Is it right to use the same port to create a second connection to the device?
Why does it work from the same process, but does not work from another?
Eventually, can I pass the socket to another node Worker process. How?
Thanks a lot!
Your server is listening on port 8888. That's the port that all clients need to connect to. The client will also have to know what the server's IP address is in order to connect to it. A client uses a target IP address and target port to specify the destination for a TCP connection.
socket.remotePort is not the port that anyone can connect on. That is the outgoing port that the first client used to connect to your server port. The outgoing port is a client bookkeeping thing that helps the client keep track of which network traffic belongs to which socket and is usually randomly assigned by the client.
You read more about what remotePort is here.
For reference, a TCP connection consists of two endpoints and each endpoint has an IP address and a port. The server will need to have a known IP address and a known port that all clients will use in order to connect to it. The client will already have its own IP address. Then, during the process of making a TCP connection to a server, the client will dynamically allocate an unused port number for the communication for this socket. That port number is used by the client to keep track of which network traffic belongs to which socket. This is not a port number that anyone can connect to.
Is it right to use the same port to create a second connection to the device?
No. You can't create a connection to a client. You can only create a connection to a listening server. And, you couldn't use the client's port that belongs to that other socket either.
I've made a node program and I want to host it trough my computer, how can I go about doing so. Currently I'm using "require("net");" function to start the server locally!
var net = require("net");
var server = net.createServer();
server.on("connection", function(socket){
//Stuff happens in here
});
server.listen(process.env.PORT || 6969, function(){
console.log("Server is listening to %j",server.address());
});
To run a Node.js application you have to install Node.js - you can download it HERE. Then in the command line / terminal, you navigate to the location of your code, and run Node.js with your file as an argument:
cd /c/some/location/with/your/file
node file.js
There is now a Node.js process that is listening on your chosen port - so it's hosted locally.
Looking at the Node.js documentation for Server.listen, it looks like the app is already listening for outside connections. The signature (or one of the signatures) is server.listen(PORT, HOST);. And I also see:
If host is omitted, the server will accept connections on the unspecified IPv6 address (::) when IPv6 is available, or the unspecified IPv4 address (0.0.0.0) otherwise.
So your app should already be contactable from the outside (although you may have to open the port explicitly depending on firewall rules). So some other computer, if they make a TCP/or whatever request to <the IP address of your computer>:6969 then the server will respond.
I'm actually surprised that the default host is 0.0.0.0 and not the loopback address (localhost or 127.0.0.1 - these are the same address).
Note not my answer look it up here:
https://serverfault.com/questions/271824/node-js-is-not-accessible-from-external-ips-on-ubuntu
You cant access node.js from outsiede because it is listening on localhost (127.0.0.1). You need to configure it to listen on 0.0.0.0, with this it will be able to accept connections on all the IPs of your machine.
server.listen({
host: '0.0.0.0',
port: 6969
});
If i understood your question correctly, you want to host your node app with your computer as a web server. It's pretty easy you can follow this blog.
Basically the steps include:
Getting a Domain name.
Registering your IP with DNS so that your
domain name points to your computer when someone enters that in the
browser.
Running the web server (Node) to handle the requests.
Note: You might face some problems if your ISP(Internet Service
Provider) changes your IP address frequently.
Since you clarified your question is about how can you host your application so other people can use it, it's not really Node-specific.
You have to open the appropriate ports in your system-level firewall and in your router as well (search for your router's model and you'll see it). Web applications always run in port 80 (for HTTP) and port 443 (for HTTPS), both in TCP. Do note that some providers won't allow you to host a server in port 80, so you'll have to try it out to check if that's the case for you.
Your Node application seems to be running in port 6969, so you either set it to use port 80 (which requires admin privileges) or use a reverse proxy to send all traffic to/from port 80 to port 6969. You can do that with nginx, for example.
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 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
I am having problems with my node.js web server.
My server is listening on port 80 and I can connect to it through localhost:80 but I can't connect through my domain name.
I have a free domain name that is pointing to a dynamic DNS since I have a dynamic IP address. I installed the program needed to update my IP address.
Am I doing something wrong or am I missing something?
I'm guessing this is to do with your server definition.
The default server.listen given in the examples..
server.listen(1337, "127.0.0.1");
Will only listen to connections from localhost. To get it to respond to any request try the following (the host part is optional)
server.listen(1337);
Because of NAT, your computer is not accessible from the internet. Your router is the only device that is accessible, and the only device that has an IP on the internet.
But your router has the ability to forward all data that someone sends to it to another computer in your local network. So if you want to make your computer accessible from the internet, you have to do such a forwarding. You must define it in your routers settings.
Hope this helps.
2 Things to try.
use this, if you haven't
app.listen(3000, "0.0.0.0")
In db.js file, go for this URI:
DB_URI = "postgresql://localhost/database_name";