Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Javascript Node.js and Socket.IO broadcast server

Javascript Node.js and Socket.IO broadcast server


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 ( 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.


  • 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.



I made it:

node.js code:

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

console.log('Socket.IO version: ' + require('').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');


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'); 
            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:


public class Client {

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

    public static void sendMessage(Socket s, String message) throws IOException {

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

html code:


How it works:

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

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


Problem courtesy of: David Benko


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:


$: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. :-)

Solution courtesy of: EhevuTov


View additional discussion.

This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Javascript Node.js and Socket.IO broadcast server


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription