Execute Javascript function between two Backing bean methods
<p>I'm writing a JSF web application who follow the next steps:</p>

<ol>
<li>Read a number from an input text</li>
<li>Look for the number in a legacy system</li>
<li>Generate a message and send It to a websocket</li>
<li>Get the response from the websocket</li>
<li>Store the result in a database</li>
</ol>

<p>All of these steps must be executed in a single event, all of these tasks are executed in a backing bean, the problem is to execute the javascript to call the websocket and use this result to save it in the database.</p>

<p>I tried to use the <code>RequestContext.getCurrentInstance().execute("function();");</code> method, but this function <strong>"Executes script after ajax request completes or on page load"</strong> and I need to get the result to store it into the database and execute other operations.</p>

<p><strong>The question is, How and where can I handle these calls?</strong></p>

<p>NOTE:</p>

<p>I can see the following options, but I'm not sure how to implement them:</p>

<ul>
<li><p>Handle the calls using Javascript instead of backing bean, following the next tasks:</p>

<ol>
<li>from Javascript call a backing bean method to look the number and get the response.</li>
<li>from Javascript get the result of the backing bean and send it to the websocket.</li>
<li>from javascript receive the response from the websocket and call another backing bean to store the result into the database.</li>
</ol></li>
<li><p>Found a way to call a javascript function from the backing bean before the ajax request completes.</p></li>
<li><p>Call the WebSocket in the Backing bean instead of using Javascript.</p></li>
</ul>

<p>I hope you can help me, Thanks.</p>

<p><strong>----EDIT-----</strong></p>

<p>The websocket is running in localhost, in this way the web page handle a device in a local machine, so it must be executed in the Javascript side and the 3rd option is not possible.</p>