Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.io on Heroku: client-side code

I've been having trouble loading the socket.io library for my JS client code on a Node.js app hosted on Heroku.

For now, I have this line at the end of my index.html file:

<script type="text/javascript" src="/socket.io/socket.io.js"></script> 

But then, when I do socket = io.connect('http://myherokuapp');in my JS client code, I get an expected 'io is not defined' error.

Any idea how to correctly load the library on Heroku?

Thanks!

like image 939
Johanisma Avatar asked Nov 10 '11 14:11

Johanisma


People also ask

Does Heroku allow WebSockets?

It gives you the flexibility of a TCP connection with the additional security model and meta data built into the HTTP protocol. For more details on the WebSocket protocol refer to RFC 6455, which is the version supported on Heroku.

Is Socket.IO same as WebSocket?

What Socket.IO is​ Socket.IO is a library that enables low-latency, bidirectional and event-based communication between a client and a server. It is built on top of the WebSocket protocol and provides additional guarantees like fallback to HTTP long-polling or automatic reconnection.


1 Answers

Ok so I finally found my way through. I'm sharing in case it helps someone.

I load the script in index.html this way:

<script type="text/javascript" src="http://myapp.herokuapp.com/socket.io/socket.io.js"></script>

It makes sense because the client-side library is actually loaded from the node server and shouldn't be pushed manually.

In my client-side JS file, I instantiate the socket this way:

socket = io.connect('http://myapp.herokuapp.com/');

Also, and this goes beyond the scope of this question, but you can't use websocket on Heroku for now. They have a little note about that here.

Hope this helps!

like image 127
Johanisma Avatar answered Oct 07 '22 01:10

Johanisma