Summary
This section going to explain how socket.io
is used in frappe and we can use the frappe Realtime
Feature in ERPNext
.
Reference Document
Get Started
You can code from the Custom App, Server Script & Client Script its depends on the requirement.
Here is the scenario Assume you have a doctype with the name of Less Hour Request from that context the below code will be there. you can implement this any doctype have to change the values accordingly.
frappe.publish_progress
We have to use this feature in Python file or Server Script in the site.
This progress will published while save but this code writen in Python file in custom app. please adjust the code for the Server Script.
less_hour_request.py
import frappe
import time
from frappe.model.document import Document
class LessHourRequest(Document):
def validate(self):
frappe.enqueue_doc(
self.doctype,
self.name,
"set_publish_progress_bar", # name of the controller method
queue="long",
timeout=4000,
param="My Discription"
)
def set_publish_progress_bar(self, **kwargs):
for i in range(1,101):
time.sleep(1)
description = f"frappe.publish_realtime {i} and Keyword Argument {kwargs}"
frappe.publish_progress(i, title='Some title', description=description, doctype=self.doctype)
output
frappe.realtime.on
The frappe.publish_progress
is completely different we are updating the realtime progress directly to the Client from the Server but frappe.realtime.on
is completely different we can use this for lots of purposes. it will send the data to the front end periodically and we can use for different scenario like User Tying
, Some One Seeing
…etc.
In this section we have to code in Python & Javascript
in javascript
less_hour_request.js
frappe.ui.form.on('Less Hour Request', {
refresh: function(frm) {
frm.trigger("set_progress_bar_1")
},
set_progress_bar_1(frm){
frappe.realtime.on('custom_event', (data) => {
let percent = Math.floor((data.current * 100) / data.total);
let message = data.message
frm.dashboard.show_progress(__('Import Progress'), percent, message);
})
}
});
In Python
less_hour_request.py
import frappe
import time
from frappe.model.document import Document
class LessHourRequest(Document):
def validate(self):
frappe.enqueue_doc(
self.doctype,
self.name,
"set_publish_progress_bar", # name of the controller method
queue="long",
timeout=4000,
param="My Discription"
)
def set_publish_progress_bar(self, **kwargs):
for i in range(1,101):
time.sleep(1)
description = f"frappe.publish_realtime {i} and Keyword Argument {kwargs}"
frappe.publish_realtime('custom_event', {'message': f'{i}% of the preocess done from 100%', 'current':i, 'total':100}, user=frappe.session.user)
Everything is same as in the frappe.publish_progress but here we used the frappe.publish_realtime this is the only diffrence. below line is only changed
frappe.publish_realtime('custom_event', {'message': f'{i}% of the preocess done from 100%', 'current':i, 'total':100}, user=frappe.session.user)
Output
frappe.realtime.off(‘event_name’)
Stop listening to an event you have subscribed to. You have to code in the Javascript or Client Script
Source Code:
Contact
Github : Antony-M1 · GitHub
Linkedin : Antony
Please support with your likes and command