[How To] Enable Socketio on web pages

Socketio is available in desk view. Any time frappe.publish_realtime() is called on server it emits js to client

e.g. Can be used for showing a pop-up to a logged in desk user when REST endpoint is accessed.

To enable this on any web page, the page should contain following lines for appropriate library to be used :

frappe.require("assets/frappe/js/lib/socket.io.min.js");
frappe.require("assets/frappe/js/frappe/socketio_client.js");
if (window.dev_server) {
    frappe.boot.socketio_port = '9000' //use socketio port shown when bench starts
}
frappe.socket.init();

on the server’s bench console enter

frappe.publish_realtime(event='msgprint', message='Hello Socketio', user='Guest')

if event='eval_js' instead of event='msgprint' the message argument will be eval() as js, e.g Following code will have same effect like the previous popup.

frappe.publish_realtime(event='eval_js', message='frappe.msgprint("Hello Socketio");', user='Guest')

16 Likes

I tried this but it only works in production. I have also noticed that the ListView in desk doesn’t auto refresh when a new doctype is posted while in development, but no issue in production. Could this be related?

Is it possible to run javascript method using socket.io?

if event='eval_js' instead of event='msgprint' it’ll run the JavaScript available on client.

try this, It works on development mode.

Use Developer forum for further discussion

3 Likes