Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8651101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:58:48+00:00 2026-06-12T13:58:48+00:00

Currently I am writing data in JSON format, one message at a time like

  • 0

Currently I am writing data in JSON format, one message at a time like this:

String[] arr = strLine.split(",");

   out.append("data: {\n");
   out.append("data: \"c1\": " + arr[0].toString()+ ",\n");
   out.append("data: \"c2\": " + arr[1].toString()+ ",\n" );
   out.append("data: }\n\n");
   out.flush();

But, now I want to out.flush only when 5 such messages are there. So, how do I do this? create Json messages? with separate data:{ } body. And if I do so how do I access pairs of c1, c2 at client side with javascript? And what are the alternatives.

PS: When I say 5 values, I mean 5 pair of values of c1 and c2.

And how do I access values using javascript at client side, if I do something like this

out.append("data: [\n");
for(int j=0; j<5; j++) {
     String[] arr = strLine.split(",");
     if (j!=0) out.append(",\n");
     out.append("\{"c1\": " + arr[0].toString()+ ",\n");
     out.append(\"c2\": " + arr[1].toString()+"}" );
     out.flush();
  }
//HEY MR, LOOK BELOW ME
//HEY MR, LOOK BELOW ME
//HEY MR, LOOK BELOW ME
//HEY MR, LOOK BELOW ME
out.append("]\n"); <---- HEY RIGHT HERE 
//HEY MR, LOOK ABOVE ME
//HEY MR, LOOK ABOVE ME
//HEY MR, LOOK ABOVE ME
//HEY MR, LOOK ABOVE ME
out.flush();

Working JS code as requested ( calling registerSSE() on body load):

function registerSSE(){
  var source = new EventSource ('http://localhost:8080/SSE_Test1/ReadCsv');
  source.addEventListener('message', function(e) {
  var data = JSON.parse(e.data);
  console.log(data.timestamp, data.c1, data.c2, data.c3, data.c4);
}

And in servlet you just need to append this line:

response.setContentType("text/event-stream");
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:58:49+00:00Added an answer on June 12, 2026 at 1:58 pm

    Please don’t manually generate/parse JSON strings. In Java, use org.json and in Javascript, use JSON.parse and JSON.stringify.

    MDN: parse and stringify

    For outputting pairs of 5, you’d do something like:

    String[] arr = strLine.split(",");
    JSONArray array = new JSONArray();
    for(int i=0, j=0;i<arr.length;i+=2,j++){
       JSONObject dataObj = new JSONObject();
       dataObj.put("c1", arr[i]);
       dataObj.put("c2", arr[i+1]);
       array.put(j, dataObj);
       if ((j > 0) && (j % 5 ==0)) {
           out.append("data: " +array.toString() + "\n");
           out.flush();
           array = new JSONArray(); // start with a new one
       }
    }
    

    To access it on the client side, given your EventSource source:

    source.addEventListener('message', function(e) {
        var data = JSON.parse(e.data); // data is an array of {c1:...,c2:...} objects
        for (var i = 0; i < data.length; i++) { // it should be 5 in length
            console.log('c1 is: '+data[i].c1+' and c2 is '+data[i].c2);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently writing an iPhone application that sends and receives JSON data from
I am currently writing a caching system that will hold serialized (json) data on
I'm currently writing an application using Vala that requires me to send data over
I'm currently writing a java program that requires some Data to run. The data
I am currently writing a program using Weka that builds a model (using one
I have a model like this: public PurchaseOrder { [Required] [StringLength(15)] public virtual string
I'm currently writing a script that exports data from a list of tables and
I'm currently writing an app that posts some data to a HTML form and
I am writing an Open Data Table to aggregate like counts from various social
I am currently writing the unit tests for a php data access object. Obviously

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.