view raw
Exhy Exhy - 11 months ago 269
Node.js Question chat example heroku

I've been trying to learn node.js and and completed the example at 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:

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=""></script>
<script src=""></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('')(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 =;
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 == {
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 app",
"dependencies": {
"express": "^4.10.2",
"": "^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.


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 '' was loaded over HTTPS, but requested an insecure script ''. This request has been blocked; the content must be served over HTTPS. Uncaught ReferenceError: $ is not defined

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

<script src=""></script>

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

<script src="//"></script>