Define a trigger for execution after a client has disconnected. If the trigger function causes an error, the error is logged but otherwise is ignored. The trigger is invoked while the session associated with the client still exists and can access session properties, such as box.session.id().
Since version 1.10, the trigger function is invoked immediately after the disconnect, even if requests that were made during the session have not finished.
- trigger-function (function) – function which will become the trigger function
- old-trigger-function (function) – existing trigger function which will be replaced by trigger-function
nil or function pointer
If the parameters are (nil, old-trigger-function), then the old trigger is deleted.
If both parameters are omitted, then the response is a list of existing trigger functions.
Details about trigger characteristics are in the triggers section.
tarantool> function f () > x = x + 1 > end tarantool> box.session.on_disconnect(f)
After the following series of requests, a Tarantool instance will write a message using the log module whenever any user connects or disconnects.
function log_connect () local log = require('log') local m = 'Connection. user=' .. box.session.user() .. ' id=' .. box.session.id() log.info(m) end function log_disconnect () local log = require('log') local m = 'Disconnection. user=' .. box.session.user() .. ' id=' .. box.session.id() log.info(m) end box.session.on_connect(log_connect) box.session.on_disconnect(log_disconnect)
Here is what might appear in the log file in a typical installation:
2014-12-15 13:21:34.444  main/103/iproto I> Connection. user=guest id=3 2014-12-15 13:22:19.289  main/103/iproto I> Disconnection. user=guest id=3