tj / axon

message-oriented socket library for node.js heavily inspired by zeromq
MIT License
1.5k stars 155 forks source link

provide requesting socket to callback #136

Closed fatihky closed 9 years ago

bretcope commented 9 years ago

This would have the unintentional consequence of breaking code where the callback is assumed to be arguments[arguments.length - 1]. I would vote against merging this unless it's an opt-in configuration option, and even then, passing the socket after the callback feels very weird, not to mention that you really shouldn't be using the underlying net.Socket since the purpose of axon is to abstract that. However, there should probably be some way of getting the read-only properties from the socket like remote address/port, so I can see the need in that respect.

Also note that RepSocket is only one type of socket server in axon. Whatever solution is decided on should be as consistent as practical across the server types (might not apply to all types).

@fatihky you really should describe your intent and use case in the pull request rather than leaving it blank.

fatihky commented 9 years ago

I'm creating rpc service and I needed to know who is sent the request. Because I'm giving some data(unique keys etc..) to them and I'm using it. Sorry my English is not good. So I will copy my code here. (note: pMongo is a simple data store)

var Rpc = require("axon-rpc")
  , axon = require('axon')
  , rep = axon.socket('rep')
 , pMongo = require('./pMongo') ;

var rpc = new Rpc.Server(rep);
rep.bind(4000);
rep.on('connect', function (sock)
{
    clients.add(sock);

    sock.on('close', function()
    {
        console.log('socket closed. ( uuid:', this._id, ')');
        clients.remove(this);
    });
});

function ClientStore()
{
    this.store = new pMongo();
}

ClientStore.prototype.add = function(sock)
{
    var added = this.store.add({sock: sock});
    sock._id = added._id;
};

ClientStore.prototype.remove = function(sock)
{
    this.store.remove({_id: sock._id});
};

// set or get role of socket
ClientStore.prototype.role = function(sock, role)
{
    var q = {_id: sock._id};
    if(typeof role == "string")
        this.store.update(q, {role: role});
    else {
        return this.store.find(q)[0].role;
    }
};

var clients = new ClientStore();

It's my code. I need to socket in rpc's handler functions. For example:

rpc.expose('role', function(role, fn, sock)
{
    clients.role(sock, role);
    fn(null, true);
});
navaru commented 9 years ago

@fatihky can you please close the issue, as it's no longer valid?

fatihky commented 9 years ago

Of course.