Category Archives: Javascript

Using socket.io in heroku environment

Socket.IO enables real-time bidirectional event-based communication. It works on every platform, browser or device, focusing equally on reliability and speed. Here is a chat application that uses socket.io to understand the concepts of socket.io.

I have deployed the application in Heroku [Heroku is a cloud platform as a service (PaaS) supporting several programming languages. Heroku various programming language like Java, Node.js, Scala, Clojure, Python and PHP and Perl.] For more details of how to deploy your NODE JS app in Heroku , click here.

Express JS [Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications] & socket.io is brought as dependencies through

package.json

"dependencies": {
"express": "4.8.5",
"socket.io": "1.0.6"
}

The package.json also lists the path from heroku will fetch the code and deploy in its environment & the node engine to be used for this application:
"engines": {
"node": "0.10.x"
},
"repository": {
"type": "git",
"url": "https://github.com/AnirbanKundu/node-chat-app"
}

Heroku looks for the

“script”

property in package.json for the initial set of instructions to run once the app is been deployed
"scripts": {
"start": "node server.js"
}

The application consists of a server.js file which listens to a predefined port.

var port = process.env.PORT || 5000;

The complete source code of the application can be found here

The WIKI page lists how to use Heroku environment.

A live version of the application can be found here.

To test the chat functionality, open multiple instances of chrome/firefox/safari and login with a unique username and start chatting. 🙂

twitterlinkedin