Frappe CLIbash

How to Set Up Socket.IO for Real-Time Events in Frappe Framework?

Socketio Setup

Learn how to configure and enable Socket.IO in your Frappe/ERPNext instance using `bench setup socketio` to facilitate real-time communication and notifications.

Setting up Socket.IO in the Frappe Framework is essential for enabling real-time features like live notifications, progress bars, and collaborative document editing. This process involves configuring Node.js, Socket.IO, and Redis, which are managed by the Bench CLI. The following commands handle the initial setup and ensure the necessary services are running correctly under Supervisor.

While Bench automates much of this, understanding the components involved helps in troubleshooting. This setup is typically performed once during the initial server configuration or when real-time functionality is first introduced into an application.

bash
bench setup socketio
sudo supervisorctl restart node-socketio

sudo touch /tmp/supervisor.sock
sudo chmod 777 /tmp/supervisor.sock

Understanding This Code

What It Does

This set of commands configures and starts the Socket.IO service for a Frappe bench, enabling real-time communication between the server and connected clients.

When To Use

Use these commands during the initial setup of a Frappe/ERPNext production environment, or when you need to enable real-time features in an existing instance. It's also used when troubleshooting Socket.IO connection issues.

Prerequisites

  • A working Frappe bench
  • Supervisor installed and configured for Frappe
  • Root or sudo access to the server

Key Concepts

Important ideas to understand in this code

Bench CLI

Bench is the command-line interface for managing Frappe Framework sites. It automates tasks like installation, setup, updates, and process management. The `bench setup socketio` command specifically configures Nginx, Supervisor, and Redis for Socket.IO integration.

Learn more

Socket.IO

Socket.IO is a library that enables real-time, bidirectional, and event-based communication between web clients and servers. In Frappe, it's used for features like live notifications, form updates, and background job progress indicators.

Learn more

Supervisor

Supervisor is a client/server system that allows its users to monitor and control a number of processes on UNIX-like operating systems. Frappe uses it to manage background workers, the scheduler, and the Node.js Socket.IO service, ensuring they are always running.

Learn more

Step-by-Step Tutorial

Follow along to understand how this code works

1

Run the Bench Socket.IO Setup Command

Navigate to your bench directory (e.g., `cd ~/frappe-bench`). This command automatically configures the necessary services like `node-socketio` and creates the required configuration files for Supervisor and Nginx.

bash
bench setup socketio
Next Step
2

Restart the Socket.IO Service

After the setup is complete, you need to restart the `node-socketio` service using `supervisorctl`. This applies the new configuration and starts the service. The `sudo` prefix is required as Supervisor is typically run by the root user.

bash
sudo supervisorctl restart node-socketio
Next Step
3

(Optional) Fix Supervisor Socket Permissions

In some environments, particularly after a fresh Supervisor installation, the bench user might not have permission to communicate with the Supervisor daemon. These commands create the socket file and grant universal read/write/execute permissions to resolve potential `EACCES` errors. This is a common troubleshooting step.

bash
sudo touch /tmp/supervisor.sock
sudo chmod 777 /tmp/supervisor.sock

Common Issues & Solutions

Troubleshoot problems you might encounter