The server part is on java, you need to notify the user about the event on the server and change something on the page. In the query model, the answer seems there is no such possibility? I have many users (5-100) connected to the jsp page, but only one of them makes some changes (some js ) and the rest should see it in their browsers on the same page. I can solve this problem using Java Servlet API given that the volume of updated data is a very small string of 2 characters. Or are there any nekostilnye ways to solve this problem?

    2 answers 2

    Yes it is possible. You should use the mechanism of web sockets , it allows you to send data to the client part without a request from it to the server. Try to start this example first and then adapt it for your needs. I see a solution like this:

    • When each user opens a page, a web socket is created and registered on the server;
    • After making changes, the server part sends some active signal to all active web sockets, upon receipt of which the browser, for example, simply refreshes the page (or other actions you need to perform). " change something on the page " can be javascript.
    • Do not forget to close unused web sockets, for example, when receiving a signal to close a user session. Here is an example of a session event listener.

    I cannot give a complete solution with the code, because I don’t have it, but you can ask SO new questions about emerging issues.

    • one
      You missed the first part in the word "web socket" - Pavel Mayorov
    • According to the written mechanism of sockets is used in the cortex, when the web server itself starts .. perhaps a веб-сокет ? - raviga
    • and just vzrrer leave this post that it is better to use ajax if sockets and why -> [here] [1] [1]: toster.ru/q/295830 - raviga
    • @Dima Khodan - firstly, in the question on the link you specified there is no uniquely correct answer (at least, the author of the question did not choose the correct answer). Secondly, the proposed answers do not contradict my answer here; moreover, they rather support it. The author of the question here is a situation in which the client needs to receive information about the changes. There are two main options to do this - either constantly polling the server by loading it (for example, every 1-5 seconds), or simply waiting for a message from the server without performing any requests until a data change message is received - bobzer
    • @bobzer Yes, the first example is very similar to what I need, thanks! - Pavel

    In other words, you need a "trigger". If you use Spring as a framework, the @Schedule(cron = "* * * * * *") annotation @Schedule(cron = "* * * * * *") will help. Send a packet of data with jason and from the js side change the page using ajax or you can for example use angularJS . The JSP itself cannot change dynamically since this is the page that is generated on the server. But you can add ajax requests to the JSP and then manually parse the jason that comes in and insert it into the already generated html ( jsp ). But also, if you need to periodically somehow change the UI depending on the actions on the server and send a request, you can use JavaRX .

    • and if I'm on clean servlets without a spring? - Pavel
    • one
      @Pavel on the first page of Google issued quite a lot of example. I think it can be found calmly when you know what to look for. - raviga
    • one
      In the context of this task, AJAX is not the most effective option. Changes are made only by one user, but at the same time all 1-100 clients will perform a continuous polling of the server, and they will have to do this even if there are no changes. The absolute majority of traffic and server load will be used idly. - bobzer
    • one
      @bobzer what? You don't seem to understand how ajax works and what it is. when you use an angular, do you also have a large amount of traffic idling? And under the hood there is the same 'ajax'. - raviga
    • @Dima Khodan maybe there is a way not to do extra calls with ajax, but this is suitable for those who are good at js and I need to shift the maximum logic to java. But thank you, I will know that ajax is not simple and can not only in the request-answer paradigm work ... - Pavel