Intra Widget Gossip

Tribler Widgets support a way of gossiping with other widget instances of the same widget type on other peers. This way, eventually every widget instance can be synchronised with each other. This tutorial shows you how to use the Intra Widget Gossip. Note that to be able to save gossiped data, you should use the local storage feature,

Using Gossip to create a ShoutBox

First, one should enable gossiping by setting the variable _enable_gossip within the widget class and probably set a Local Storage structure. (The local storage structure will likely be of the same format as the gossip message):

    _enablegossip = True

    _localstorage_structure = [("name":"text"),("shout":"text"), ("clock":"integer")]

In the __init__ method, you should initialize your ShoutBox GUI, which we will not treat here. Also, the OnPostInit method should be implemented to initialize the GUI with your database data, in this case displaying the last 10 shouts of the database (see LocalStorage).

In our example, we create a simple listbox with columns "name" and "shout" and use the "clock" variable only to create a simple "happened before" relation between the shouts, to order on. Underneath the listbox is an add field to add your own shouts. The widget should handle the add function and add it to the database.

Implement the gossiping

To start gossiping shouts, we need to implement 2 functions: OnCreateGossipMessage(self) and OnHandleGossipMessage(self, gossip_message.

OnCreateGossipMessage should return a dictionary, which is the latest gossip message. For example:

    def OnCreateGossipMessage(self):

        shouts = self._db.getAll(("name", "shout", "clock"),limit=10, order_by = "clock DESC")

        message = {'shouts': shouts}

        return message

OnHandleGossipMessage is called when the gossip engine has gotten a gossip message for this widget and is called with the gossip message. For the houtbox, we insert them into the database and refresh the GUI.

    def OnHandleGossipMessage(self, gossip_message):

        self._db.insertOrIgnoreMany(gossip_message.get("shouts", []))


That's it! Only 3 simple things to remember to use gossiping: _enablegossip, OnCreateGossipMessage and OnHandleGossipMessage!

Attached is a fully implemented ShoutBox to show you how things interchange.