Ask coding questions

← Back to all posts
Connect to replit ip of my server
Necrodragon41

Hi! I'm trying to make a websocket server and I've got the server's IP.

When I connect to my server by domain i get 172.18.0.49, that is the server's IP. Now, if I want to connect by ip, I get The site took too much time to answer (translated). Can I connect by IP?

Answered by 19wintersp [earned 5 cycles]
View Answer
Voters
Necrodragon41
Comments
hotnewtop
19wintersp

The IP you're trying to connect to isn't the correct one. IPv4 addresses which start with 172 are private network addresses, so it won't work properly. According to whois, the actual IP is 35.201.120.147, though that points to repl.co. So as far as I know, no, you can't connect by IP. May I ask why you are trying to?

Necrodragon41

@19wintersp yeah. But what I'm trying to do is this:
I want to make a server for my application, but (dont know why) it does not connect to the application if I connect it by the project.username.repl.it. What I wanted is to open a web server on port 5000 and on the index show the current ip, so to be able to connect like ip:1234 that is the port of the websocket. (The appñication is an android application, using socketio (websockets))

19wintersp

@Necrodragon41 It should be <repl>.<username>.repl.co.

Also, you don't need to specify the ports in this way as it's handled by Replit, and I think socket.io handles the connection. I'd personally prefer raw WebSockets over socket.io, as that would let you do the configuration yourself.

Necrodragon41

@19wintersp so i dont need to do sometjing like repl.name.repl.co:port?

19wintersp

@Necrodragon41 Not that I know of; the port is related to the protocol. Again, I don't know much about socket.io, so I don't know how it works internally.

Necrodragon41

@19wintersp not necesarily on socket.io. When I try to connect to it from chrome using the port 5000, I cannot. But if I try connecting to it by port 80 I am able, although I specifies port 5000

19wintersp

@Necrodragon41 Yes, that's because your server isn't actually being exposed. It's running in a Docker container on Replit's servers, with specific port forwarding. HTTP is on port 80, HTTPS on port 443, etc. No matter what port you expose in your server code, Replit will fix it for you.

Necrodragon41

@Necrodragon41 does that mean that i connect automatically to port 1234 if i use websockets?

19wintersp

@Necrodragon41 No, WebSocket is built on HTTP(S) (I think), so it uses port 80 (443).

Necrodragon41

@19wintersp ok. But then why i get the error if I use the same port twice, one for web and the other for socketio, but then they can both go to port 80? Its like strange because why would replit know if i want the websocket or the website?

Necrodragon41

@Necrodragon41 or it would redirect websockets to port 80 and website to 443? (Or the inversr)

19wintersp

@Necrodragon41 A port can only be used by one app at a time. Most WebSocket libraries allow you to attach the WebSocket server to a HTTP server to make it work correctly with both. The reason Replit knows is to do with the way the protocols work. A WebSocket request will have a special header which tells the server to use a WebSocket connection instead.

19wintersp

@Necrodragon41 No, port 80 is for HTTP over TCP (HTTP), port 443 is for HTTP over TCP with TLS (HTTPS).

Necrodragon41

@19wintersp all doubts cleared! Thanks!

DexieTheSheep

@19wintersp not OP but do you know a way to prevent this? I don't want to add "exception code" and make the code unnecessarily complicated just to test on replit

19wintersp

@DexieTheSheep I'm not exactly sure what you mean. The consequence of using Replit is that you are necessarily sandboxed. If you want to be able to access these listening ports externally, you have to only use one port, and that protocol has to be HTTP.

DexieTheSheep

@19wintersp bruh :/