Deep-dive on the Next Gen Platform. Join the Webinar!

Skip Navigation
Show nav
Dev Center
  • Get Started
  • Documentation
  • Changelog
  • Search
  • Get Started
    • Node.js
    • Ruby on Rails
    • Ruby
    • Python
    • Java
    • PHP
    • Go
    • Scala
    • Clojure
    • .NET
  • Documentation
  • Changelog
  • More
    Additional Resources
    • Home
    • Elements
    • Products
    • Pricing
    • Careers
    • Help
    • Status
    • Events
    • Podcasts
    • Compliance Center
    Heroku Blog

    Visit the Heroku Blog

    Find news and updates from Heroku in the blog.

    Visit Blog
  • Log inorSign up
Hide categories

Categories

  • Heroku Architecture
    • Compute (Dynos)
      • Dyno Management
      • Dyno Concepts
      • Dyno Behavior
      • Dyno Reference
      • Dyno Troubleshooting
    • Stacks (operating system images)
    • Networking & DNS
    • Platform Policies
    • Platform Principles
  • Developer Tools
    • Command Line
    • Heroku VS Code Extension
  • Deployment
    • Deploying with Git
    • Deploying with Docker
    • Deployment Integrations
  • Continuous Delivery & Integration (Heroku Flow)
    • Continuous Integration
  • Language Support
    • Node.js
      • Working with Node.js
      • Troubleshooting Node.js Apps
      • Node.js Behavior in Heroku
    • Ruby
      • Rails Support
      • Working with Bundler
      • Working with Ruby
      • Ruby Behavior in Heroku
      • Troubleshooting Ruby Apps
    • Python
      • Working with Python
      • Background Jobs in Python
      • Python Behavior in Heroku
      • Working with Django
    • Java
      • Java Behavior in Heroku
      • Working with Java
      • Working with Maven
      • Working with Spring Boot
      • Troubleshooting Java Apps
    • PHP
      • PHP Behavior in Heroku
      • Working with PHP
    • Go
      • Go Dependency Management
    • Scala
    • Clojure
    • .NET
      • Working with .NET
  • Databases & Data Management
    • Heroku Postgres
      • Postgres Basics
      • Postgres Getting Started
      • Postgres Performance
      • Postgres Data Transfer & Preservation
      • Postgres Availability
      • Postgres Special Topics
      • Migrating to Heroku Postgres
    • Heroku Key-Value Store
    • Apache Kafka on Heroku
    • Other Data Stores
  • AI
    • Working with AI
  • Monitoring & Metrics
    • Logging
  • App Performance
  • Add-ons
    • All Add-ons
  • Collaboration
  • Security
    • App Security
    • Identities & Authentication
      • Single Sign-on (SSO)
    • Private Spaces
      • Infrastructure Networking
    • Compliance
  • Heroku Enterprise
    • Enterprise Accounts
    • Enterprise Teams
    • Heroku Connect (Salesforce sync)
      • Heroku Connect Administration
      • Heroku Connect Reference
      • Heroku Connect Troubleshooting
  • Patterns & Best Practices
  • Extending Heroku
    • Platform API
    • App Webhooks
    • Heroku Labs
    • Building Add-ons
      • Add-on Development Tasks
      • Add-on APIs
      • Add-on Guidelines & Requirements
    • Building CLI Plugins
    • Developing Buildpacks
    • Dev Center
  • Accounts & Billing
  • Troubleshooting & Support
  • Integrating with Salesforce
  • Language Support
  • Node.js
  • Working with Node.js
  • Using WebSockets on Heroku with Node.js

Using WebSockets on Heroku with Node.js

English — 日本語に切り替える

Last updated December 04, 2024

Table of Contents

  • Create a New App
  • Option 1: WebSocket
  • Option 2: Socket.io

This tutorial gets you started with real-time Node.js applications on Heroku. We develop a simple application that shares the server’s current time with the client via a persistent socket connection. Each application is based on Node’s popular express web server.

When developing real-time Node.js applications, you can use WebSockets directly. You can also use an abstraction library like Socket.io, which provides fallbacks for clients that don’t support the WebSocket protocol. We demonstrate both options.

Using dynos to complete this tutorial counts towards your usage. We recommend using our low-cost plans to complete this tutorial. Eligible students can apply for platform credits through our new Heroku for GitHub Students program.

Create a New App

Go to your app’s directory, and create a default package.json.

$ npm init --yes

Specify a version of Node in package.json, and provide a mechanism for starting the app.

"engines": {
  "node": "22.x"
},
"scripts": {
  "start": "node server.js"
}

Option 1: WebSocket

The simplest way to use WebSocket connections is directly through Node’s ws module. We cover each step in setting up the app, but you can also view the full source on GitHub.

Install Dependencies

Let’s start with a basic express web server.

$ npm install --save express

For WebSockets, we install the ws and the bufferutil and utf-8-validate modules. Only the ws module is necessary, but the bufferutil and utf-8-validate modules provide a performance boost.

$ npm install --save ws bufferutil utf-8-validate

Create an HTTP Server

We need an HTTP server to do two things: serve our client-side assets and provide a hook for the WebSocket server to monitor for requests. The server code looks like:

const PORT = process.env.PORT || 3000;
const INDEX = '/index.html';

const server = express()
  .use((req, res) => res.sendFile(INDEX, { root: __dirname }))
  .listen(PORT, () => console.log(`Listening on ${PORT}`));

Create a WebSocket Server

The WebSocket server takes an HTTP server as an argument so that it can listen for events.

const { Server } = require('ws');

const wss = new Server({ server });

Handle Connections

We listen for and log connections and disconnections. After a client connects, you can add event handlers for messages from that client. The server code looks like:

wss.on('connection', (ws) => {
  console.log('Client connected');
  ws.on('close', () => console.log('Client disconnected'));
});

Broadcast Updates

One of the benefits of socket connections is that your server can broadcast data to clients without waiting for client requests. In this case, we push the current time to all clients every second.

setInterval(() => {
  wss.clients.forEach((client) => {
    client.send(new Date().toTimeString());
  });
}, 1000);

Create a WebSocket Client

Our client, index.html, is a simple HTML page that listens for time updates from the server. The client code can go between <script> tags in the HTML, and it looks like:

var HOST = location.origin.replace(/^http/, 'ws')
var ws = new WebSocket(HOST);
var el;

ws.onmessage = function (event) {
  el = document.getElementById('server-time');
  el.innerHTML = 'Server time: ' + event.data;
};

Start the App

You can now start the server.

$ npm start
> node server.js

Listening on 3000

Test the app locally at http://localhost:3000 to confirm that the time is updated in real time. You also see Client connected in your server logs.

When you’re satisfied with the behavior, commit all your files to git, except node_modules, which you want to add to .gitignore. Then deploy the app to Heroku.

$ heroku create
$ git commit -am 'websocket starting point'
$ git push heroku main
$ heroku open

Option 2: Socket.io

A real-time abstraction library like Socket.io can help your app serve users without WebSocket support. Socket.io also provides common functionality like rooms, namespaces, and automatic reconnection. We cover each step in setting up the app, but you can also view the full source on GitHub.

Install Dependencies

This app requires a basic express web server and socket.io.

$ npm install --save express socket.io

Create an HTTP Server

We need an HTTP server to do two things: serve our client-side assets and provide a hook for Socket.io to monitor for socket.io-related requests. The server code looks like:

const PORT = process.env.PORT || 3000;
const INDEX = '/index.html';

const server = express()
  .use((req, res) => res.sendFile(INDEX, { root: __dirname }))
  .listen(PORT, () => console.log(`Listening on ${PORT}`));

Create a Socket.io Server

The Socket.io server takes an HTTP server as an argument so that it can listen for socket.io-related requests.

const io = socketIO(server);

Handle Connections

We log clients connecting and disconnecting. After a client connects, you can add event handlers to the SocketIO instance for receiving client messages.

io.on('connection', (socket) => {
  console.log('Client connected');
  socket.on('disconnect', () => console.log('Client disconnected'));
});

Broadcast Updates

One of the benefits of persistent socket connections is that the server can push data to clients without waiting for a client’s request. In this example, we push the current time on the server once per second.

setInterval(() => io.emit('time', new Date().toTimeString()), 1000);

Create a Socket.io Client

Our client, index.html, is a HTML simple page that listens for time updates from the server. The client code can go between <script> tags in the HTML, and it looks like:

var socket = io();
var el;

socket.on('time', function(timeString) {
  el = document.getElementById('server-time')
  el.innerHTML = 'Server time: ' + timeString;
});

Start the App

You can now start the server.

$ npm start
> node server.js

Listening on 3000

Test the app locally at http://localhost:3000 to confirm that the time is updated in real time. You also see Client connected in your server logs.

When you’re satisfied with the behavior, commit all your files to git, with node_modules in .gitignore. Then deploy the app to Heroku.

$ heroku create
$ git commit -am 'socket.io starting point'
$ git push heroku main
$ heroku open

You want apps using Socket.io to enable session affinity. If you plan to use Node’s Cluster module, or to scale your app to multiple dynos, follow Socket.io’s multiple-nodes instructions.

$ heroku features:enable http-session-affinity

Keep reading

  • Working with Node.js

Feedback

Log in to submit feedback.

Information & Support

  • Getting Started
  • Documentation
  • Changelog
  • Compliance Center
  • Training & Education
  • Blog
  • Support Channels
  • Status

Language Reference

  • Node.js
  • Ruby
  • Java
  • PHP
  • Python
  • Go
  • Scala
  • Clojure
  • .NET

Other Resources

  • Careers
  • Elements
  • Products
  • Pricing
  • RSS
    • Dev Center Articles
    • Dev Center Changelog
    • Heroku Blog
    • Heroku News Blog
    • Heroku Engineering Blog
  • Twitter
    • Dev Center Articles
    • Dev Center Changelog
    • Heroku
    • Heroku Status
  • Github
  • LinkedIn
  • © 2025 Salesforce, Inc. All rights reserved. Various trademarks held by their respective owners. Salesforce Tower, 415 Mission Street, 3rd Floor, San Francisco, CA 94105, United States
  • heroku.com
  • Legal
  • Terms of Service
  • Privacy Information
  • Responsible Disclosure
  • Trust
  • Contact
  • Cookie Preferences
  • Your Privacy Choices