2021.11.13 06:50 AM
I am studying the official example code from here: https://github.com/kxcontrib/websocket/blob/master/AppendixB/pubsub.q . After some research, I can get the publisher/subscriber model running and I can understand the basic idea on how the feedhandler part (i.e., this file: https://github.com/kxcontrib/websocket/blob/master/AppendixB/fh.q) works. However, I am still very confusing on how the publisher part (i.e., this file: https://github.com/kxcontrib/websocket/blob/master/AppendixB/pubsub.q) works.
I understand that when the code is initially executed, it listens on port 5001 and it defines a few tables (trade, quote, subs) and functions (loadPage, filterSyms, getSyms, getQuotes, getTrades, sub, pub). Does it mean that the interpreter directly jumps to the end of the file, that is, these two lines:
.z.ts:{pub each til count subs};
\t 1000
and starts something like a "main loop" in winForm development?
Suppose my guess is right, the loop keeps calling the function pub until there aren't any new rows in the table subs. But how does subs get any new rows in the first place?
2021.11.21 03:10 AM
Yes your understanding is correct for those 4 items.
These tutorial videos may be useful:
2021.11.15 01:01 AM - edited 2021.11.15 01:41 AM
.z.ts is the timer function which is evaluated on intervals of the timer variable set by system command \t
In your example 'pub' is called for each subscriber in 'subs' once per second.
To start subs has count 0 but when a new subscriber connects and subscribes they will be added.
How the subscribers are added to that table in that demo is:
2021.11.16 06:44 AM - edited 2021.11.16 06:47 AM
Thanks @rocuinneagain for taking a look at my lengthy question! Let's say we examine the flow this way:
In this particular example, I understand that information is randomly generated by fh.q; and fh.q interact with pubsub.q via these two function calls (https://github.com/kxcontrib/websocket/blob/ad2f0b268afaee1fc5f4dda2fc2467440c7e2f0c/AppendixB/fh.q#...
My confusion starts here: in lines 18 and 19 of fh.q, `upd is an alias to insert; `quote and `trade are table names, and the rest are parameters randomly generated--so which line in pubsub.q will be invoked when these two function calls are made (please correct me if the term "function call" is inaccurate)?
Thanks!
2021.11.16 07:23 AM - edited 2021.11.16 07:24 AM
TP = Tickerplant (Relays data to subscribers + recovers from logfile after crashes)
RDB = Realtime Database (Stores data for query)
RTE = Realtime engine (Performs streaming calculations and stores caches or publishes results)
(Any process can be customised)
1. Yes
2. It is acting more like a mixture between a TP,RDB,RTE
a) It does not store a logfile to recover in case of a crash (tp-logfile - a TP normally does this)
b) It stores data indefinitely instead of acting only as a relay. (Unlike a TP, more like and RDB, although an RDB will clear once every 24hrs)
c) It does not relay data untouched instead only specific data is forwarded (similar to an RTE)
getSyms - sends like of unique symbols across tables
getQuotes - sends last row by sym from quote table
getTrades - sends last row by sym from trade table
3. Yes
The execution flow is:
1. FH sends messages to PubSub (lines 18/19) every 100ms
2. The messages arrive to PubSub and .z.pg evaluates them. This mean upd/insert (pubsub.q line 8 will save the incoming data to quote/trade. PubSub now has some data cached.
3. The next time the PubSub timer (.z.ts) is triggered (every second) the 'pub' function will trigger and send data to subscriptions.
This code is a basic demo so it may have some holes in it's logic (like never clearing data in PubSub so eventually memory will run out)
2021.11.18 07:38 AM - edited 2021.11.21 03:14 AM
Hi @rocuinneagain , thanks for the detailed reply and some informative clarification!
But I still have some confusion...the crux is this sentence you wrote
2. The messages arrive to PubSub and .z.pg evaluates them. This mean upd/insert (pubsub.q line 8 will save the incoming data to quote/tr...
I understand that .z.pg is a function under the .z namespace, my questions are:
Thanks!
2021.11.21 03:10 AM
Yes your understanding is correct for those 4 items.
These tutorial videos may be useful:
EMEA
Tel: +44 (0)28 3025 2242
AMERICAS
Tel: +1 (212) 447 6700
APAC
Tel: +61 (0)2 9236 5700
KX. All Rights Reserved.
KX and kdb+ are registered trademarks of KX Systems, Inc., a subsidiary of FD Technologies plc.