Search code examples
javascriptnode.jssocket.ioapache-kafkakafka-producer-api

How to create socket io connection between 2 servers


I have 2 servers one is UI server and other is kafka server.

The javascript file in my UI server is getting data from a csv file which i'm reading line by line and converting into JSON. I need to send these line by line read data in JSON format to my Kafka producer server. for further working. Both the server have there own dedicated IP addresses. e.g. kafka server has 192.168.2.12:9098

reportJSON is the variable on which i'm getting my csv data in UI server js file.

When i'm trying to run js file of ui server it is showing error:

2018-05-09T15:18:56.147Z - error: uncaughtException: io.connect is not a function date=Wed May 09 2018 15:18:56 GMT+0000 (UTC)

UI connection inside the JavaScript file:

var io = require('socket.io');
var socket = io.connect("http://192.168.2.12:9098");
    socket.on('connect', function () {
                                console.log('Connection Established');
                                socket.emit('csvDataFromUI', function (reportJSON) {
                                    console.log("Data inside the csvUpload Handler is = " + reportJSON);
                                });
                            });

code inside the kafka producer javaScript file:

var http = require('http');
var app = express();
var host = process.env.HOST || config.host;
var port = process.env.PORT || config.port;

console.log("STARTING EVENT SERVER PRODUCER");

    var server = http.createServer(app).listen(port, function () { });
    server.timeout = 240000;
    var io = require('socket.io').listen(server);
    io.on('connection', function (socket) {
            socket.on('csvDataFromUI', function(data) {
                    console.log("Data in kafka is   = " + data);
                            });             
            //socket.emit('csvDataFromUI', payloadData);   
    });

/*************************************************************************/ New code: following this: https://www.npmjs.com/package/kafka

UI server ui.js

creating it as a producer:

var kafka = require('kafka');
var host = '192.168.2.12';
var port = 9098;
producer = new kafka.Producer({

          host:         host,
          port:         port,
          topic:        'Postings',
          partition:    0
 });
producer.connect(function(reportJSON) {
      console.log("rportJSON = " + reportJSON);
      producer.send(reportJSON);
});

Kafka server kafkaProducer.js:

var kafkadata = require('kafka');

    console.log("STARTING PRODUCER");

    var consumer = new kafkadata.Consumer({
        // these are the default values
        host:         '192.168.2.12',
        port:          9098 ,
        pollInterval:  2000,
        maxSize:       1048576 // 1MB
    })
    consumer.on('message', function(topic, message) { 
        console.log(message)
    })
    consumer.connect(function() {
        consumer.subscribeTopic({name: 'Postings', partition: 0})
    })

ERROR I'm getting in UI server is: error: uncaughtException: connect ECONNREFUSED reportJSON = undefined

In Kafka server I'm can't see any receiving and getting ERROR: ReferenceError: message is not defined


Solution

  • It looks like the function io.connect does not come from the npm socket.io. It comes from client side portion of socket.io. So traditionally you would use the socket.io npm to host your socket based server and then use the client side socket io JS lib to connect and communicate with said server.

    But you want to do it server to server. According to this Stack Overflow question, the last answer indicates that when you npm install socket.io it will host a version automatically in your node_modules. You might be able to do something like:

    var socketIoClient = require('socket.io-client');
    var socket = socketIoClient.connect("http://192.168.2.12:9098");
    

    EDIT: It does look like they export the client side code as a module so it should be possible to do what I mentioned above. I was worried the client side JS would not translate to server side as a module.