{
    "componentChunkName": "component---src-templates-doc-page-js",
    "path": "/docs/en/JS_API/session_lifetime_control/",
    "result": {"data":{"site":{"siteMetadata":{"title":"Gatsby-doc-engine"}},"markdownRemark":{"id":"1fdf4733-f8c7-516c-91ec-891bd463b5fc","excerpt":"Session lifetime control?> Session — a sequence of client and bot interactions within a single conversation context. A session begins with the first client…","html":"<h1>Session lifetime control</h1>\n<hr>\n<p class='warn'>Session — a sequence of client and bot interactions within a single conversation context.</p>\n<p>A session begins with the first client request if there is no other active session for this client.</p>\n<p>Reasons for a session termination:</p>\n<ul>\n<li>timeout expiry;</li>\n<li>device turning on/off;</li>\n<li>solving a client issue that is being confirmed by filling in a form for feedback etc.</li>\n</ul>\n<p>The requirements for a session termination are unique for every bot. Requirement configuration is made at the script level. The script is supposed to determine the moment of the old session ending and the moment of the beginning of a new one. It is possible thanks to the <a href=\"/1.10.3/docs/en/JS_API/built_in_services/reactions/newSession\"><code class=\"language-text\">$reaction.newSession</code></a>.</p>\n<p>The <code class=\"language-text\">newSession</code> reaction throws a specific exception. When such an exception is caught the log is written and a new session is created.</p>\n</br>\n<h4>Methods of session control</h4>\n<p>You can use the methods below to control the session:</p>\n<ul>\n<li><a href=\"/1.10.3/docs/en/JS_API/built_in_services/reactions/newSession\"><code class=\"language-text\">$reactions.newSession</code></a> create a new session from the script;</li>\n<li><a href=\"/1.10.3/docs/en/JS_API/built_in_services/jsapi/startSession\"><code class=\"language-text\">$jsapi.startSession()</code></a> starts a new session;</li>\n<li><a href=\"/1.10.3/docs/en/JS_API/built_in_services/jsapi/stopSession\"><code class=\"language-text\">$jsapi.stopSession()</code></a> close a session.</li>\n</ul>\n</br>\n<h4>How to use</h4>\n<ul>\n<li>Creating a new session by timeout.</li>\n</ul>\n<div class=\"gatsby-highlight\" data-language=\"sc\"><pre class=\"language-sc\"><code class=\"language-sc\"><span class=\"token keyword\">theme:</span> /\n    <span class=\"token keyword\">init:</span>\n        bind(\"postProcess\", function($context) {\n            $context.session.lastActiveTime = $jsapi.currentTime();\n            log($context.session.lastActiveTime)\n        });\n \n        bind(\"preProcess\", function($context) {\n            if ($context.session.lastActiveTime) {\n                var interval = $jsapi.currentTime() - $context.session.lastActiveTime;\n                if (interval > 10000) {\n                    $reactions.newSession( {message: 'test 6', session: $context.request.data } );\n                }\n            }\n        });</code></pre></div>\n</br>\n<ul>\n<li>Here is an example of how a new session can be triggered by an event.</li>\n</ul>\n<div class=\"gatsby-highlight\" data-language=\"sc\"><pre class=\"language-sc\"><code class=\"language-sc\">    <span class=\"token keyword\">state:</span> sessionStart\n        <span class=\"token storageType\">event!:</span> newChatStarted\n        <span class=\"token variableParameter\">script:</span>\n            $request.query = $request.rawRequest.messages[0].text;\n            $request.event = null;\n            $reactions.newSession({request: $request});</code></pre></div>\n<p>The script switched to a state triggered by the <code class=\"language-text\">newChatStarted</code> event. We need to create a new session but we also must handle the client’s message that accompanied the event.</p>\n<p>We override <code class=\"language-text\">$request.event = null</code> in order to avoid switching to the state once again. We also write the client’s message to <code class=\"language-text\">$request.query</code>. Then we create a new session (<code class=\"language-text\">$reactions.newSession</code>) with the client’s message already stored.</p>\n</br>\n<ul>\n<li>Here is an example of a script that raises the <code class=\"language-text\">newSessionException</code> error.</li>\n</ul>\n<div class=\"gatsby-highlight\" data-language=\"sc\"><pre class=\"language-sc\"><code class=\"language-sc\"><span class=\"token keyword\">state:</span> reset\n    <span class=\"token storageType\">event!:</span> someEvent  \n    <span class=\"token variableParameter\">script:</span>\n        $reactions.newSession({message: \"some text\", data: $request.data});</code></pre></div>\n<p>Here, the script switched to a state triggered by the <code class=\"language-text\">newChatStarted</code> event that will be saved to <code class=\"language-text\">$request.event</code>. We create a new session, replacing the text. However, we keep the original event. Events are primary triggers for the platform, so we will get into an endless loop in this script.</p>\n<p>To avoid this, override the <code class=\"language-text\">$request.event = null</code> event.</p>","frontmatter":{"title":"","description":null},"headings":[{"value":"Session lifetime control"}]}},"pageContext":{"slug":"/docs/en/JS_API/session_lifetime_control/","previous":{"fields":{"slug":"/docs/en/NLU_core/advanced_classifer_settings/"},"frontmatter":{"title":"","description":null}},"next":{"fields":{"slug":"/docs/en/JS_API/preload_js/"},"frontmatter":{"title":"","description":null}}}},
    "staticQueryHashes": ["1209419333"]}