POS Longpolling¶
It is a custom odoo module made by IT-Projects LLC, which allows sending instant updates to the POS interfaces from backend.
It provides following methods in Backend side:
self.env['pos.config].send_to_all_poses(channel_name, data)
: broadcasts messages to all opened POSes (see example)pos_set._send_to_channel(channel_name, data)
: broadcasts message to the POSes inpos_set
(see example)_send_to_channel_by_id(self, dbname, pos_id, channel_name)
: sends message to exact POSpos_id
, uses data base namedbname
,channel_name
,message='PONG'
(see example)
Note
POS will get notification only if it’s subscribed to the specified channel_name
.
For Client side the methods are:
add_bus(key, sync_server)
: allows to create additional Bus to sync data with another Sync Server (see example inpos_multi_session
- it gets data from local server to speed up synchronization)
Note
You don’t need to use add_bus
if you connect with your regular odoo server.
add_channel_callback: function(channel_name, callback, thisArg)
: subscribes to specific channel (see example)
Let’s check example of usage taking as a basis Sync Partners in POS
module:
Sync Partners in POS module¶
This module on each partner update (in Backend) notifies POSes to update partner data.
Here you can see how it uses pos_longpolling
:
BACKEND¶
- On partner update
send_field_updates
method. is called:
@api.model
def send_field_updates(self, partner_ids, action=''):
channel_name = "pos_partner_sync"
data = {'message': 'update_partner_fields', 'action': action, 'partner_ids': partner_ids}
self.env['pos.config'].send_to_all_poses(channel_name, data)
- It uses
send_to_all_poses
method.
CLIENT¶
- On POS starting it’s subscribed to channel
pos_partner_sync
.
initialize: function () {
PosModelSuper.prototype.initialize.apply(this, arguments);
var self = this;
this.ready.then(function () {
self.bus.add_channel_callback("pos_partner_sync", self.on_barcode_updates, self);
});
},
- On notification on_barcode_updates is called, which reloads partner data:
on_barcode_updates: function(data){
var self = this;
if (data.message === 'update_partner_fields') {
var def = new $.Deferred();
if (data.action && data.action === 'unlink') {
// partner is deleted. Make necessary updates in UI
this.remove_unlinked_partners(data.partner_ids);
def.resolve();
} else {
// reload partner data
def = self.load_new_partners(data.partner_ids);
}
return def.done(function(){
var opened_client_list_screen = self.gui.get_current_screen() === 'clientlist' && self.gui.screen_instances.clientlist;
if (opened_client_list_screen){
// rerender partner list
opened_client_list_screen.update_client_list_screen(data.partner_ids);
}
});
}
},