using ngrok with websocket (or socket.io)
Asked Answered
D

4

34

Are there any simple sample code showing ngrok forwarding socket.io/websocket (running under nodejs on localhost)?

In other words, does

ngrok http 3000

work with a nodejs server and socket.io running on port 3000? Or something like

ngrok http+tcp 3000 (just wild guessing)
Disabled answered 9/7, 2015 at 15:9 Comment(1)
you should add your solution as an answer and accept it, it will be clearerKempf
B
11

Yes. If your node app is working off of port 3000 like in your example then just use ngrok to create a reverse proxy to expose 3000 to the world. Websockets will work just fine with it.

Bromic answered 19/7, 2017 at 20:55 Comment(0)
B
5

If you want to use the socket on ngrok then you just need to create the ngork link using

ngrok tcp 3000

//and hit you Socket API endpoint like: 
tcp.ngrok.io:15819/api/v1/partner/profile?partner_id=27
Boffin answered 18/9, 2023 at 13:52 Comment(0)
A
4

(Self-answer moved from the question into the answers section)

simply use

ngrok http 3000

You will see the web address string that localhost binds to. So in the client javascript code, change

var socket = io.connect('http://localhost:3000');

to

var socket = io.connect('http://94349fe6.ngrok.io');

NOTE:

http://94349fe6.ngrok.io is just that web address string. Yours will be different from this one.

Actually, if one simply does:

var socket = io();

as the introduction to socket.io states:

it defaults to trying to connect to the host that serves the page

So it works as well.

Armure answered 9/7, 2015 at 15:9 Comment(1)
I'm trying to establish connection through JavaScript's WebSocket constructor and it says that the protocol needs to be ws:// or wss://.Regenerate
M
-2

Try using LocalTunnel:

Usage: lt --port [num] <options>

https://www.npmjs.com/package/localtunnel

Megen answered 21/8, 2022 at 8:11 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.