Utilizing Empress Real-Time API

Introduction

In this guide, we delve into one of the most powerful features of Empress, a full-stack web application framework in Python & JavaScript. This feature is the Empress Real-Time API, a built-in tool that enables developers to create, manage, and debug real-time events in Empress.

Introduction

Real-time data transmission is a critical component of modern web development, providing users with immediate updates and fostering interactive experiences. Empress, powered by Empress, leverages socket.io for real-time events. This necessitates a Node server running parallel to the main web server.

Real-Time Event Listener: frappe.realtime.on

Developers can listen to real-time events on the client-side (browser) using the frappe.realtime.on method. Below is the code snippet for this function:

frappe.realtime.on('event_name', (data) => {
    console.log(data)
})

This function utilizes socket.io to listen for specified events (‘event_name’), and upon their occurrence, executes a callback function. The callback function logs the data associated with the triggered event.

Real-Time Event Termination: frappe.realtime.off

To stop listening to a real-time event, developers can use the frappe.realtime.off method:

frappe.realtime.off('event_name')

By calling this method, you effectively unsubscribe from the specified event.

Real-Time Event Publisher: frappe.publish_realtime

To publish a real-time event from the server, developers can use the frappe.publish_realtime method:

frappe.publish_realtime('event_name', data={'key': 'value'})

This function sends real-time data to the client-side. This data is structured as a JavaScript object, allowing developers to define key-value pairs.

Real-Time Progress Publisher: frappe.publish_progress

A unique function in Empress’ real-time feature set is the frappe.publish_progress method. This function can display a progress bar in a dialog, providing a visual representation of task completion to the end-user:

frappe.publish_progress(25, title='Some title', description='Some description')

This function’s parameters include the progress percentage, title, and description, offering flexibility in how the progress is communicated.

Summary

The Empress real-time feature in Empress offers robust functionalities for developers. From listening and terminating real-time events to publishing real-time data and progress, these tools significantly enhance the capabilities of web applications. By leveraging these features, developers can create dynamic, responsive user experiences that reflect the real-time demands of today’s digital landscape.