Exhy Exhy - 1 year ago 495
Node.js Question

socket.io chat example heroku

I've been trying to learn node.js and socket.io and completed the example at http://socket.io/get-started/chat/. I added some extra features and it works on localhost. Now I'm trying to deploy this to a server on heroku but I can't get it to work.

I don't have enough reputation to show the main things I've read. I looked at the articles on heroku "Getting Started on Heroku with Node.js", "Deploying Node.js Apps on Heroku" and "Using WebSockets on Heroku with Node.js" but I still can't figure out what to do.

The html page shows on my app but the chat doesn't work: https://salty-ridge-74778.herokuapp.com/

Here is what I have so far:


<!doctype html>
<title>Socket.IO chat</title>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages li { padding: 5px 10px; }
#messages li:nth-child(odd) { background: #eee; }
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
var socket = io();
socket.emit('chat message', $('#m').val());
return false;
socket.on('chat message', function(msg){
socket.on('user connected', function(name){
$('#messages').append($('<li>').text(name + " connected"));


var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

var nextUserId = 0;
var users = [];

app.set('port', (process.env.PORT || 5000));

app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');

io.on('connection', function (socket) {
var socketId = socket.id;
users.push({ 'id': socketId, 'name': "User" + nextUserId });

console.log(users[users.length - 1].name + ' joined with id ' + users[users.length - 1].id);
io.emit('user connected', users[users.length - 1].name);
socket.on('disconnect', function () {
console.log('user disconnected');
socket.on('chat message', function (msg) {
var name;
for (var x = 0; x < users.length; x++) {
if (users[x].id == socket.id) {
name = users[x].name;

io.emit('chat message', name + ": " + msg);
console.log('message: ' + name + ": " + msg);

http.listen(app.get('port'), function(){
console.log('listening on port ' + app.get('port'));


"name": "socket-chat-example",
"version": "0.0.1",
"description": "my first socket.io app",
"dependencies": {
"express": "^4.10.2",
"socket.io": "^1.4.6"
"engines": {
"node": "6.2.2"


web: node index.js



To set the app up I typed these commands into the command line once I was in the correct folder:

git init
heroku create
git add .
git commit -m '1'
heroku git:remote -a salty-ridge-74778
git push heroku master

If anyone could help I'd be eternally grateful.

Answer Source

The console shows the JavaScript errors that are causing your app to fail. Open a debug console in your browser:

Mixed Content: The page at 'https://salty-ridge-74778.herokuapp.com/' was loaded over HTTPS, but requested an insecure script 'http://code.jquery.com/jquery-1.11.1.js'. This request has been blocked; the content must be served over HTTPS. salty-ridge-74778.herokuapp.com/:25 Uncaught ReferenceError: $ is not defined

Instead of including scripts like this, where you hardcode their protocol to secure or insecure:

<script src="http://code.jquery.com/jquery-1.11.1.js"></script>

Include them like this, so they inherit the protocol of the hosting page:

<script src="//code.jquery.com/jquery-1.11.1.js"></script>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download