How to listen to Server POST event in Angular 5

292 views
Skip to first unread message

Saju Thankathurai

unread,
Dec 21, 2017, 11:53:27 PM12/21/17
to ang...@googlegroups.com
Hi,

I have a requirement to listen to a POST event from server and display the payload information in UI.

We have Jetty server which runs on port 8080, which will publish a JSON object to a URL, Example URL: http://localhost:8080/myRest/myRestMethod 

Below is the piece of server code which sends the POST request to the above URL.

// CODE START

private void sendHTTPData(JSONObject json) {
        StringBuilder stringBuilder = new StringBuilder();
        try {
                URLConnection connection = null;
            connection = url.openConnection();
            connection.setDoInput(true);
            ((HttpURLConnection)connection).setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", APPLICATION_JSON);
            connection.setRequestProperty("Accept", APPLICATION_JSON);
            OutputStreamWriter streamWriter = new OutputStreamWriter(connection.getOutputStream());
            streamWriter.write(json.toString());
            streamWriter.flush();

            //Verify Response
            if (((HttpURLConnection)connection).getResponseCode() == HttpURLConnection.HTTP_OK){
                InputStreamReader streamReader = new InputStreamReader(connection.getInputStream());
                BufferedReader bufferedReader = new BufferedReader(streamReader);
                String response = null;
                while ((response = bufferedReader.readLine()) != null) {
                    stringBuilder.append(response + "\n");
                }
                bufferedReader.close();
                ((HttpURLConnection)connection).getResponseMessage();
                System.out.println("Received Response..... " + stringBuilder.toString());
            }
        } catch (Exception e){
            System.out.println(e.getMessage());
        } finally {
            System.out.println("Disconnecting..... HttpURLConnection");
            ((HttpURLConnection)connection).disconnect();
        }
    }

// CODE ENDs

W
​hen server sent the post request, I need to catch it in UI. So how I can listen to POST request from server?

​My UI runs in node js on port 4200 and I have a proxy connection with Jetty server as per proxy.config.json set as below
{
    "/sample-rest/*": {
      "target": "http://FGT009936567:8080",
      "secure": false,
      "logLevel": "debug"
    },
    "/myRest/*": {
      "target": "http://FGT009936567:8080",
      "secure": false,
      "logLevel": "debug"
    }
  }

​Jetty server is running on port 8080 on FGT009936567 machine.​​

--
Saju Thankathurai,

Saju Thankathurai

unread,
Dec 31, 2017, 9:09:58 PM12/31/17
to ang...@googlegroups.com
Any help please.

Happy new year 2018 guys.

Zlatko Đurić

unread,
Jan 1, 2018, 3:58:04 AM1/1/18
to Angular and AngularJS discussion
Happy New Year!

I'll try to give you a hint. Web app type clients can't listen to classic HTTP POST. In fact, webapps typically can't serve any type of HTTP requests.

If you need the server to initiate contact, look into technologies like websockets, Web Push API, server-sent events or even common long polling. All those have different properties, so study them and pick what is best for you.

Reply all
Reply to author
Forward
0 new messages