Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using ngrok with websocket (or socket.io)

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)

EDIT:

to answer my own question after help from ngrok.com

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.

EDIT AGAIN:

actually, if one simply does:

var socket = io();

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

So it works as well.

like image 648
Zhe Hu Avatar asked Jul 09 '15 15:07

Zhe Hu


People also ask

Does Ngrok work with WebSockets?

Websocket endpoints work through ngrok's HTTP tunnels without any changes. However, there is currently no support for introspecting websockets beyond the initial 101 Switching Protocols response.

Should I use Socket.IO or WebSockets?

As said before, Socket.IO can fall back to technologies other than WebSockets when the client doesn't support it. If (for some reason) a WebSocket connection drops, it will not automatically reconnect… but guess what? Socket.IO handles that for you! Socket.IO APIs are built to be easier to work with.

Is Socket.IO efficient?

js - Using socket.io is broadcasting the same efficiency as sending a message to every client - Stack Overflow.

Can I use Socket.IO without node JS?

Is it possible to use socket.io without any node. js dependencies? The short answer is yes. You will however have Flash dependency.


1 Answers

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.

like image 58
Shaun Sweet Avatar answered Sep 26 '22 15:09

Shaun Sweet