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 8886525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:34:48+00:00 2026-06-14T21:34:48+00:00

I just started learning nodejs. I am currently working with sockets and made chat

  • 0

I just started learning nodejs. I am currently working with sockets and made chat program.

I want to save entire chat to a json file. Currently my code is this :

  socket.on('chat', function  (data) {
    message = {user : data.message.user, message : data.message.message};
    chat_room.sockets.emit('chat', {message: message});

    jsonString = JSON.stringify(message);

    fs.appendFile("public/chat.json", jsonString, function(err) {
        if(err) {
            console.log(err);
        } else {
            console.log("The file was saved!");
        }
    }); 

  });

This is currently working perfect, but the json which is written in file is wrong.

This gave me a wrong json

{"user":"niraj","message":"hw r u?"}{"user":"ntechi","message":"hello"}{"user":"ntechi","message":"hw r u?"}

The above code is called when message is triggered. I want json in this format

{"user":"awd","message":"hw r u?","user":"ntechi","message":"hello","user":"ntechi","message":"hw r u?"}

Can anyone help me in this? Thanks in advance

  • 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-14T21:34:49+00:00Added an answer on June 14, 2026 at 9:34 pm

    The first set of wrong JSON is created because you are appending a piece of JSON to a file each time you get a message.

    The second set of JSON is also wrong – each property name has to be unique.

    Presumably you want something like:

    [
    {"user":"niraj","message":"hw r u?"},
    {"user":"ntechi","message":"hello"},
    {"user":"ntechi","message":"hw r u?"}
    ]
    

    In which case the logic you need to use is:

    1. Read data from file
    2. Parse data as JSON and assign to a variable
    3. In the event of an error, assign an empty array to that variable
    4. push the message object onto the end of the array
    5. stringify the array
    6. Overwrite the file with the new string
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just started learning C++ and am currently using codeblocks. I want to write
I just started learning java and I'm working on a program. I'm getting an
Just started learning Haskell. I have an empty source file with this inside: pe
I just started learning RoR. This is my /app/trip_controller.rb file class TripController < ApplicationController
I just started learning Backbone.js, and have been working on (what else) a simple
I just started learning Zend. I managed to get the basic working (using zf
I just started learning HTML5, I started with normal text file and changed its
I just started learning C++ and I wrote this sample program from the text
I just started learning Python... I am writing a simple program that will take
I just started learning some ruby, and I want to do something like this:

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.