Live Update custom function


#1

I wonder if its possible to had more functions into live_updates.js like for example the scheduler.updateCollection();

I’ve tested and noticed that they are not part of the file.
Is that simple to implement?

I noticed that all the functions use a name that doesn’t exist yet (scheduler.add, scheduler.changeId, etc)

I tried to create a new one called scheduler.updateSection with 2 parameters (old, new) and passed a return to this.updateCollection(old, new). This works well on the browser, however doesn’t pass to another browser like it should.

What am I missing here?

I would also like to do it for tasks colors, etc…


#2

Hi,

There is no ready method to exchange custom data between clients and live_updates.js provides no extension points that would allow you to add this functionality without modifying live_updates.js itself.

However, since live_updates.js is not a part of the official package, and the file itself is not too big, it should be not too difficult to modify it according to your needs.

Firstly, please check the latest version of live_updates.js from my post here Live updates addEvent error

In order to send custom data, you’ll need to expose the Faye client (https://faye.jcoglan.com/) used in live_updates.js to the rest of your app so you could call it from code.
It can be done here:

live_updates.js, line:26

	var client = new Faye.Client(url);
	client.subscribe('/update', function (message) {
		self.applyChanges(JSON.parse(message));
	});

	this.attachEvent("onLocalUpdate", function (data) {
		client.publish('/broadcast', JSON.stringify(data));
	});

You can add something following:

	var client = new Faye.Client(url);
	client.subscribe('/update', function (message) {
		self.applyChanges(JSON.parse(message));
	});

	this.attachEvent("onLocalUpdate", function (data) {
		client.publish('/broadcast', JSON.stringify(data));
	});

	// added two methods of the scheduler object:
	scheduler.publish = function(channel, message){
		client.publish(channel, message);
	};
	scheduler.subscribe = function(channel, callback){
		client.subscribe(channel, callback);
	};

Then in your app you could send the data to all connected clients:

scheduler.publish("/sections", JSON.stringify({
	collection: "users",
	value: [...]
}));

And receive it

scheduler.subscribe("/sections", function(message){
	var action = JSON.parse(message);
	scheduler.updateCollection(action.collection, action.value); 
	// or reload collections from the server
}));

I believe you won’t have to change anything on the server in order to make it work.
One thing to keep in mind is that the page that sends the message using ‘publish’ will also receive it’s own message in ‘subscribe’.