Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Persistent and asynchronous stream for communication between appengine/Spring and front-end

At the moment my website is using Spring that handles the http(s) request to and from the front-end like this:

@RestController
public class ComputeController {

    @RequestMapping(value = "/api/compute", method = RequestMethod.POST)
    public String compute(@RequestBody CodeToken code, OAuth2Authentication OAuth2) {
        Map<String, String> userInfo = UserInformation.getUserInfo(OAuth2);

        String sourceCode = code.getSource();
        String filename = code.getFilename();
        String email = userInfo.get("email");

        try {
            DataStorage dateStorage = new DataStorage();
            Compiler compiler = new Compiler(dateStorage);

            return compiler.compile(filename, sourceCode, email);
        } catch (Exception e) { // TODO Don't catch all exceptions
            return e.getStackTrace().toString();
        }
    }
}

The problem is that I need my front-end (built in Angular) to be able to receive and send information asynchronous from the http(s) request sent from the front-end. Like an continuous I/O stream from the server mid request while the "compiler.compile(...)" is running.

I presume I need to use sockets for this but I'm looking for suggestion on a good way to implement them.

like image 459
Nehliin Avatar asked Dec 02 '25 21:12

Nehliin


1 Answers

If I understand your intention correctly, you're trying to display some progress in your client while the code compiles. You have two options:

  1. As you proposed, use WebSockets. Spring supports them well. You can see an example here: https://github.com/AlexeySoshin/SpringWebSockets/tree/master/src/main/java/com/alexeysoshin/chat
  2. Instead of blocking your response,
    1. Do compilation on a separate thread pool.
    2. Assign each compilation UUID when you submit this task.
    3. Return the client this task immediately.
    4. Poll another endpoint with UUID
like image 187
Alexey Soshin Avatar answered Dec 05 '25 10:12

Alexey Soshin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!