2

I am new to Nodejs and am trying to set up a server client connection using sockets. Below is my code. Server is working OK but client is not connecting. Please can anyone help me figure out the mistake.

Much Thanks jessi

Server.js

var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);

io.on('connection', function(socket){
  console.log('a user connected');
  socket.on('disconnect', function(){
    console.log('user disconnected');
  });
});

io.on('data', function(data) {
  console.log('DATA from client is: ' + data);
  // Close the client socket completely

});
server.listen(4200);
console.log('Monitoring server listening on port 4200');

Client.js

var HOST = '127.0.0.1';
var PORT = 4200;
var express = require('express');
var app = express();
var client = require('http').createServer(app);
var io = require('socket.io')(client);

client.connect(PORT, HOST, function()
{

  console.log('CONNECTED TO: ' + HOST + ':' + PORT);
  // Write a message to the socket as soon as the client is connected,
  // the server will receive it as message from the client
  io.write('I am Chuck Norris!');

});

// Add a 'data' event handler for the client socket
// data is what the server sent to this socket
client.on('data', function(data) {

  console.log('DATA: ' + data);
  // Close the client socket completely
  client.destroy();

});

// Add a 'close' event handler for the client socket
client.on('close', function() {
  console.log('Connection closed');
});

1 Answer 1

2

For the client you use the socket.io-client package instead. The client side doesn't require the use of the Express portion since you're not recreating a web server on the client. If you look at your current code you're essentially recreating the Socket server which isn't what you want to do.

All that is necessary is to create a new Socket.io client and register your various event handlers.

var socket = require('socket.io-client')('localhost:4200');
socket.on('data', function(data) {
  // handle incoming data
  console.log(data);
});
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks a lot for your help.
@JessiAnnGeorge No problem. Just make sure to mark this as the answer if it indeed help you so others will know it was the answer when viewing this question in the future.
What I have to implement is 2 servers(a provisioning and monitoring server) that need to talk to each other. Am not sure how to do this so I made one as the Server and the other as the client and talk that way. Client will be requesting services from the provisioning server and for soem requests, the provisioning server will need to get the data from monitoring server. How can we achieve this using 2 servers ?thanks a lot
@JessiAnnGeorge you can use socket.io-client within your socket.io server. By doing so you can have a Socket.io server that is also a client to a separate Socket.io server. This is the setup that you are discussing in this comment. If you have specific questions regarding the implementation I recommend that you open up a separate question where people can answer it. This is outside the scope of the original question.
Thanks again, i think i found my answer..Much thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.