Ad

Client Side Can Not Find Io

- 1 answer

I have written a simple program that has a nodejs consumer, consuming data from kafka and then it transfers data using socket.io to a client so that I may render the data using d3.

However i keep running into this error on the client side that it can not find io.

I have tried to use a cdn and other alternatives in my index.html however it has not resulted in any progress.

Consumer.js

var options = {
  fromOffset: 'latest'
};

var express = require('express');
var router = express.Router();

// Kafka-node initilizations.
var kafka = require('kafka-node'),
 Consumer = kafka.Consumer,
 client = new kafka.KafkaClient(),
 consumer = new Consumer(
     client,
     [
         { topic: 'trial1', partition: 0 }
     ],
     [
 {
   autoCommit: false
 },
 options =
 {
   fromOffset: 'latest'
 }
     ]
 );


 consumer.on('message', function (message) { 

     router.get('/', function(req, res, next) {
       res.render('index', {title: 'Test', condition: false});
     });
     console.log(message.value);
 });


// Display error messages if any.
consumer.on('error', function (err) 
{
  console.log('ERROR' + err.toString());
});

Client.js

 //init socket 
var socket = io();
    //on message call update 
    socket.on('message', function(message){
      console.log(message)
 });

index.html

<html>
 <head>
   <script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
   <script src="https://code.jquery.com/jquery-1.11.1.js"></script>
 </head>
 <body>
   <script>
     var socket = io.connect();
   </script>
   <div>This is our socket.html file</div>
 </body>
</html>
Ad

Answer

Your version appears to be slightly outdated, and older versions of Socket.IO only have the server version and the client used to be separated (socket.io-client.js used to be a separate package).

Make sure you're using the correct version of Socket.IO (you can get from https://cdnjs.com/libraries/socket.io)

Like So:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js"></script>

Also make sure you use this format:

<script>
  var socket = io('my.ip'); // NOT io.connect();
  ... // Rest of code
</script>

Hope I was of help!

Ad
source: stackoverflow.com
Ad