Agus
Agus

Reputation: 1624

Generating socket.io at Server side as Rest request

There is a way to manage the socket.io creation at Server Side?, Currently, I couldn't found any doc, Only found in relation with the socket is created per request from a client "io.connect(server)".

The current flow work OK:

  1. Set Socket.io (at Node.js) at SERVER:PORT
  2. Client connect to SERVER
    • using io.connect(SERVER:PORT)

I wonder if it is possible ? Trying to do:

  1. Set Socket.io (at Node.js) at SERVER:PORT
  2. Recieved a POST (REST) - Server side
    • Create/Open Socket.io a server side.
    • At response of Post send the id?
  3. the clien open a socke.io

Sent to client socket.id to client as

Upvotes: 1

Views: 1622

Answers (3)

Agus
Agus

Reputation: 1624

So Far, looking in deep on the code and doc, I found that socket.io support namespaces, so I used this in order to manage client connection id.

at server.js

  var app = express();
    var server = require('http').createServer(app),
    io = require('socket.io').listen(server,{ log: false });
    // Rest New Process
    function generateNameSpaceWs (responce, request) { 
       io.of("/" + id).on('connection', handler);
      response.send(id);
    }
    app.post("/newWS", function (res, req) {
       return generateNameSpaceWs(res, req);
    }

at Client.js

function makeWS(){
    var ws, c = new XMLHttpRequest();
    c.open("GET", url, false);
    c.send();
    if (c.status == 200){
        id = JSON.parse(c.responseText);
         ws = new io.connect("server/" + id)
}

Upvotes: 2

John Henry
John Henry

Reputation: 3223

This doesn't seem possible -- while the official documentation for socket.io is lacking, the documentation for the net module indicates that the only way to create a socket is to initiate it server side. However, you can still achieve the desired effect by creating an id for the socket on the server to associate with the socket. That is,

  1. Set Socket.io (at Node.js) at SERVER:PORT
  2. Recieved POST (REST) - Server side
  3. Create id (Note:This could be done before step 2)
  4. At response of Post send the id!
  5. Client connect to SERVER
    • using io.connect(SERVER:PORT)
  6. The client sends the id to the server using something like

    socket.emit("set_id",id)
    
  7. The server recieves the id and associates it with the socket using something like

    socket.on("set_id",function(id){
        socket.set("id",id)
    }
    
  8. Now you can reference the socket using the id that you created!

Good luck!

Upvotes: 0

user568109
user568109

Reputation: 48003

So far you are doing right, if I understand your question correctly, you are trying to authenticate connection via POST, so that user can only connect to server via socket if server responds to ID. This is a roundabout way. Use the socket instead of POST.

Socket server has to be running already, and accepts connection via io.sockets.on('connection'), at server you can choose whether to accept it or reject it ,do socket.disconnect('unauthorized') to close connection from server.

I would you suggest you do this :

  1. Set Socket.io (at Node.js) at SERVER:PORT
  2. Client connect to SERVER using io.connect(SERVER:PORT)
  3. Send what you are sending in POST over socket.
  4. Authenticate/Process on io.sockets.on('connection', function(socket) at server.
  5. Close socket if unathorized.
  6. Send back ID data to client.

Upvotes: 0

Related Questions