The short answer is:
Yes. It is perfectly possible to make this happen. You do not need any specific application like Apache. All you need is a router, know your Public IP and redirect, on your router, the packets coming from Public IP to a specific TCP port for the internal IP of the computer that is running the Chat server. This port forwarding is critical, unless the chat is running on the router itself.
The long answer is:
On the internal network, if the computers are on the same subnet, it is easy to get them to communicate. This is your current case.
However, when you want to communicate with a computer that is inside your home, from a computer that is outside your home, things get a little more complicated. The bottom line is that in order to communicate in this way, you need to know your Public IP, that is, the IP that the provider provides you so that you can browse the Internet from your home.
It is very common for the provider to also provide you with a router as this allows multiple devices that are connected to your home network, whether over Wi-Fi or wire, to browse the Internet simultaneously. In addition to knowing your Public IP, you need to know in which TCP port your chat is listening (you already know).
To better understand, let's call the computer outside your EXT home and the computer inside your INT home. The INT is connected to your home router, here called ROT .
EXT - External Computer - Hypothetical Public IP: 200.123.12.21
ROT - Router (or gateway) that connects INT to the Internet. IP Hypothetical public: 179.79.12.12. Internal IP: 192.168.1.1
INT - Internal Computer - Hypothetical Internal IP: 192.168.1.23. TCP port hypothetical that the chat server is listening on: 3131
It basically works like this:
Goal: EXT You want to connect to a chat service by running on the INT machine that is in the @PascalStarting home;
-
EXT needs to know the public IP of ROT .
-
EXT You also need to know the port that the chat service is listening on.
-
EXT then, with the chat client, connects to 179.79.12.12.12:3131.
- The packet sent by EXT goes through multiple routers until you reach ROT .
- At the time this packet arrives in ROT it is redirected to port 3131 on Internal IP 192.168.1.23 (the INT IP)
-
INT then receives this packet, processes and responds. (As INT responds and this package comes back in EXT I leave it for you to search.)
Basically that's it. There are interesting issues to consider, such as security and availability as the other response said. Another very important issue is that providers constantly change their Public IP, therefore having a Dynamic DNS or Dynamic DNS (DDNS) service configured on the router is fundamental.
As a way of learning, I strongly recommend that you try this, as much will be learned in the process.
As a matter of curiosity, I have a scheme like this in my house. I can connect a computer remotely, work and then turn it off, also remotely. However, since I'm boring with security, I use SSH Port Forward. My router is a TP-Link N750, running OpenWRT (Linux), so I can have SSH and other services that allow me to safely manage my home network.