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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:19:22+00:00 2026-06-16T13:19:22+00:00

I’m trying to utilise TCP sockets in Node.js to communicate with a Lua program.

  • 0

I’m trying to utilise TCP sockets in Node.js to communicate with a Lua program. There are two issues I’m trying to get my head around. Firstly buffers.

Buffers As I understand it, when data is provided to your code from the socket, it will be whatever data has been received so far (a stream rather than packets). So when you read the data received, it may be broken from what was sent.

i.e. data received
{ schools : [“Long

vs data sent
{ schools : [{“Longwood”, “Hillbrow”}] }

The way to get around this is to put your data into a ‘buffer’ and split it by whatever method you use to show the end of that piece of information. Commonly appears to be new line.

My questions here:

  1. Can you end up with more 2 or more complete sections of data in the buffer, how would you handle this? For loop?
  2. The same issue appears to be present with data leaving the socket. I have noticed however in other code examples, people use the Node.js Buffer before writing to the socket. Why is this not used for incoming data?
  3. If more data is written than can be handled to leave, does Node handle this for you or do you need to come up with a method.

Finally I seem to be misunderstanding the data side. Does all data sent and received need to be converted to binary and back? I wish to send JSON data back and forth only. I think there in lies my confusion. For example:

var myQuestion = "Is this acceptable and will I encounter any issues?
socket.write(myQuestion);

Many thanks for your time.

  • 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-16T13:19:23+00:00Added an answer on June 16, 2026 at 1:19 pm
    1. Yes, you could definitely end up with several sections of data. It entirely depends on the code you write on either end.
    2. I don’t quite follow what you mean by “use the buffer before writing”. Buffers are just pure binary data, they are used for both incoming and outgoing data. Node also generally accepts strings for sending, and assumes they are UTF8 if no encoding is given. You can also get strings when receiving if you call setEncoding.
    3. The OS has buffers for incoming data on the TCP socket, and when the buffer is full (being emptied too slowly by node), it will stop accepting packets until there is space.

    First off, Buffer is binary. A buffer is simply an array of bytes, and nothing more. Buffers can be created from strings, and node will do this automatically in some cases. In your case, I would recommend that you call socket.setEncoding('utf8'). That will automatically convert incoming data into a string to simplify your parsing.

    As far as processing and splitting the data, it is up to you. TCP only provides a stream of bytes arriving in the same order they were send. As you said, you can gather up the bytes and when a newline is received, you take everything before that and parse it as JSON. This should work fine. You can use any character that wouldn’t pop up inside JSON. As long as the programs doing the JSON serialization do not add newlines, then you are set. As ‘data’ is emitted, you can check the string for newline, and if not, then add it to any previously received data, and if you find it, then split it up, add the existing data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I'm interested in microtypography issues on the web. I want a tool to fix:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I've tracked down a weird MySQL problem to the two different ways I was
I know there's a lot of other questions out there that deal with 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.