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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:07:57+00:00 2026-05-27T01:07:57+00:00

I just need a simple example how to do a Node.JS server that I’ll

  • 0

I just need a simple example how to do a Node.JS server that I’ll explain.

Basically Node.JS will have 2 servers running:
– A crude TCP server and
– A Socket.IO server
The objective is to forward data from a TCP Client to various Socket.IO clients interested in it

The reason to do this is to make easy to do the communication with other languages (I’ll have a java server sending messages in a tcp socket, as I couldnt find a better way to do this – all available java libraries (socket.io server and clients implemented in java) are buggy) as almost every language has socket api.

The TCP client will send a string right after connect and the Node.JS server will create a namespace with it and provide data for it so Socket.IO clients must be able to receive the data that the server will forward from the TCP client.

Steps:

  • Listen for TCP connections

  • On a new TCP connection receive a string from the client

  • Create a Socket.IO server with a namespace named the string provided from tcp client

  • Start receiving data from TCP client and broadcast it to all the Socket.IO clients conected to the namespace that was created when this socket opened

This has to be made in a way that various TCP clients and Socket.IO clients can communicate

I cant find how to do this in a efficient way, if someone can provide a simple example I’m sure it would help a lot of ppl (as Node.JS and Socket.IO lacks documentation) and this is easy to make for ppl that already know of the subject.

Thanks.


UPDATE:

I made it:

node.js code:

var javaPort = 8080;
var sIOPort = 8081;
var javaServer = require('net').createServer();
var browserServer = require('socket.io').listen(sIOPort);

console.log('Socket.IO version: ' + require('socket.io').version);

javaServer.on('listening', function () {
    console.log('Server is listening on ' + javaPort);
});

javaServer.on('error', function (e) {
    console.log('Server error: ' + e.code);
});

javaServer.on('close', function () {
    console.log('Server closed');
});

javaServer.on('connection', function (javaSocket) {
    var clientAddress = javaSocket.address().address + ':' + javaSocket.address().port;
    console.log('Java ' + clientAddress + ' connected');

    var firstDataListenner = function (data) {
        console.log('Received namespace from java: ' + data);
        javaSocket.removeListener('data', firstDataListenner);
        createNamespace(data, javaSocket);
    }

    javaSocket.on('data', firstDataListenner);

    javaSocket.on('close', function() {
        console.log('Java ' + clientAddress + ' disconnected');
    });
});

javaServer.listen(javaPort);

function createNamespace(namespaceName, javaSocket) {
    var browserConnectionListenner = function (browserSocket) {
        console.log('Browser Connected');
        var javaSocketDataListenner = function(data) {
            console.log('Data received from java socket and sent to browser: ' + data);
            browserSocket.emit('m', data + '\r\n');
        }

        var javaSocketClosedListenner = function() {
            console.log('The java socket that was providing data has been closed, removing namespace'); 
            browserSocket.disconnect();
            browserServer.of('/' + namespaceName).removeListener('connection', browserConnectionListenner);
            javaSocket.removeListener('data', javaSocketDataListenner);
            javaSocket.removeListener('close', javaSocketClosedListenner);
        }

        javaSocket.on('close', javaSocketClosedListenner);
        javaSocket.on('data', javaSocketDataListenner);

        browserSocket.on('disconnect', function () {
            console.log('Browser Disconnected');
            javaSocket.removeListener('data', javaSocketDataListenner);
            javaSocket.removeListener('close', javaSocketClosedListenner);
        });
    }

    var namespace = browserServer.of('/' + namespaceName).on('connection', browserConnectionListenner);
}

java code:

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

public class Client {

    public static void main(String[] args) {
        try {
            Socket nodejs = new Socket("localhost", 8080);
            sendMessage(nodejs, "testnamespace");
            Thread.sleep(100);
            int x = 0;
            while (true)
            {
                sendMessage(nodejs, x + "");
                x++;
                Thread.sleep(1000);
                System.out.println(x + " has been sent to server");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void sendMessage(Socket s, String message) throws IOException {
        s.getOutputStream().write(message.getBytes("UTF-8"));
        s.getOutputStream().flush();
    }

    public static String readMessage(Socket s) throws IOException {
        InputStream is = s.getInputStream();
        int curr = -1;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while ((curr = is.read()) != -1) {
            if (curr == '\n') {
                break;
            }
            baos.write(curr);
        }
        return baos.toString("UTF-8");
    }
}

html code:

<html>
    <head>
    <script src="socket.io.js"></script>
    </head>
    <body>
    <script>
        var socket = io.connect("http://localhost/testnamespace", {port: 8081});
        console.log(io.version);
        socket.on('connect', function () {
            console.log('Connected');
            });
        socket.on('m', function (msg) {
            console.log('Message received: ' + msg);
        });
        socket.on('disconnect', function () {
            console.log('Disconnected');
        });
    </script>
    </body>
</html>

How it works:

Java connect no nodejs over TCP socket then send a namespace name, nodejs create a socket.io server with that namespace and forward all messages java socket sends to all socket.io clients connected to that namespace

If anyone see errors or improvements that could be made please share.

Thanks

  • 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-05-27T01:07:57+00:00Added an answer on May 27, 2026 at 1:07 am

    I’m making a similar app, but instead of using TCP I’m using ZeroMQ on top of TCP. This code works very simply.

    Check out my code here:
    https://github.com/EhevuTov/netPeek

    Do:

    $:node support/msu_gen.js
    $:node app.js
    

    I hope that helps.

    As a side note, I don’t think my callback function is properly implemented. I think I’m setting the on event object every time a message gets passed. If you find a better way, please submit pull request with a fix. 🙂

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

Sidebar

Related Questions

I just need a simple clarification: I have an example application with a Model
I need your help. I have made a really clean and simple example to
I am running a LAMP server on a VPS, and I have just installed
I'm new to subprocessing. I just need a really simple win32 example of communicate()
Just need a little bit of Javascript to warn people that press the back
I just need to know hot to create a CGLayer that has an image
Basically I just need the id of the record on the first form so
I'm working in a android project the idea is simple: I just need to
I have a simple file XML like below: <brandName type=http://example.com/codes/bmw# abbrev=BMW value=BMW />BMW</brandName> <maxspeed>
I just wanted to make sure I don't need Core Data for a simple

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.