Search code examples
kdb

callback on table update in kdb


I want to implement a client server mechanism in kdb where clients can register them self to receive a callback when some table is updated.

I know how the callback work in kdb, I was not able to figure how to bind table updates in server to a function from where I can call 'callback' from client.


Solution

  • Basically you want to implement 'Publish-Subscribe' mechanism. KDB already has a script 'u.q' in tick library which provides that: https://code.kx.com/q/cookbook/publish-subscribe/

    On server, it maintains list of clients along with their handles, subscription tables and callback functions. You will have to change function on server which handles data insert/update to also publish the data.

    q)  .u.pub[table name; table data]
    

    This will take care of calling each client's callback function which are registered for this table.

    On client side, create the connection to publisher and call the library function for subscription.

    q) .u.sub[tablename;list_of_symbols_to_subscribe_to]
    

    You can also look into example publisher and subscriber code: https://github.com/KxSystems/cookbook/tree/master/pubsub