TAGS :Viewed: 15 - Published at: a few seconds ago

[ Setting up a Stateless connection with NodeJS and Socket.IO ]

After prototyping my project using PHP and Unity3D i've decided on building the production version using Cordova and NodeJS.

I'm currently using Socket.io with NodeJS and having some confusion with connections. The way that I had expected this to work out was the following procedure:

  1. The client would connect to the server with a request
  2. The server would respond to the request
  3. The connection would be closed

However, it seems that the connection likes to stay open, and if the connection is closed, it continuously attempts to reconnect which is not what I am looking for. I'm attempting to establish a single state of data transfer, similar to what happens when you make a web-request to a PHP file.

The source code of the project is pretty much boilerplate code:

var application = require('express')();
var http = require('http').Server(application);
var server = require('socket.io')(http);

http.listen(8080, function() {
    console.log('Listening on *:8080');

server.on('connection', function(socket) {
    console.log('SERVER: A new connection has been received.');
    server.on('disconnect', function() {
        console.log('SERVER: A connection has been closed.');

I do not need a persistent connection, nor do I want one.

Thoughts: I could send a close handshake from the client. For example:

  1. Send some data to the server
  2. Recieve some data from the server
  3. Send a close request to the server / just close the socket
  4. Continue application logic once the socket is closed

Would this be the proper way to handle this? However then the question arises, what if the data gets lost, then there's a permanently open socket. Would implementing a basic timeout be ideal in this situation? (IE: If a response isn't received within 10 seconds, there was an error or the server was not available).

Answer 1

Not sure why you're using socket.io for this. Socket IO is used for different purpose and doesn't fir your criteria here. I have seen mainly its uses in real time application and binary streaming.You can try TCP socket in node.js

var net = require('net');

var HOST = '';
var PORT = 6969;

// Create a server instance, and chain the listen function to it
// The function passed to net.createServer() becomes the event handler for the 'connection' event
// The sock object the callback function receives UNIQUE for each connection
net.createServer(function(sock) {

    // We have a connection - a socket object is assigned to the connection automatically
    console.log('CONNECTED: ' + sock.remoteAddress +':'+ sock.remotePort);

    // Add a 'data' event handler to this instance of socket
    sock.on('data', function(data) {

        console.log('DATA ' + sock.remoteAddress + ': ' + data);
        // Write the data back to the socket, the client will receive it as data from the server
        sock.write('You said "' + data + '"');


    // Add a 'close' event handler to this instance of socket
    sock.on('close', function(data) {
        console.log('CLOSED: ' + sock.remoteAddress +' '+ sock.remotePort);

}).listen(PORT, HOST);

console.log('Server listening on ' + HOST +':'+ PORT);

Check out here

Answer 2

Then Socket.io is the wrong tool for your scenario. socket.io needs to keep the socket open to get events from the server back to the client (and vice-versa). As a matter of fact, even of the server does not support WebSockets, socket.io will resort back to other mechanisms, such as polling.