[SOLVED] How to get the current connection id from a publication without "this"?

In a method and publication you can easily do: this.connection.id.

However, I need to extract the current connection.id without passing “this”.

When a method is invoked, it’s easy, you can do DDP._CurrentInvocation.get().connection.id

But inside a publication that _CurrentInvocation is empty.

I need to somehow be able to extract the connection.id before a publication yields, but I don’t have access to the bound variable “this”

I don’t understand - if you’re “inside a publication”, why don’t you have access to this?

Let me give you a more concrete example.

function doSomething() {
      // how to access connection id here, provided I cannot pass "this" from publish function
}

Meteor.publish('xxx', function () {
       doSomething();
});

Maybe using some Meteor stuff ? I spent lots of hours console.logging. I read DDPServer implementation fully almost :slight_smile: For publication there doesn’t seem to be a way to do it :frowning:

Not sure how concrete your example is, but

Meteor.publish('xxx', function () {
       doSomething.call(this);
});

ensures that doSomething runs with the context of the publication.

1 Like

@robfallows I cannot pass it, or bind to it. It’s just a function call, it may be a non-direct function call. I need within the execution of a publication to extract the connection id without having access to the context. I don’t know if this is possible, I’ve looked through the meteor code but could not find anything.

Within a method it’s easy with _CurrentInvocation.get() , maybe something like that can be found for this ?

Not sure if this will help, but you could save connectionId at some other place before executing the subscription and the pick it from there (for example after login)

Accounts.onLogin(function(attempt){
    Connections[attempt.user._id]=attempt.connection._id;
});

@tcastelli what if I don’t have any user id ? Still, how would I access the user._id ?

Meteor.userId() works in methods invocation. Because I can get the _CurrentInvocation.

yeah, that was just an example of a place where you could fetch the connectionId for later use.

what about storing it on Meteor.onConnection(callback) when you receive it (on an importable object)?

Thanks to @mitar

I found out about this. This solves the issue.

1 Like