Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[subscriptions] custom onConnect #104

Closed
AndreMaz opened this issue Dec 27, 2021 · 4 comments · Fixed by #105
Closed

[subscriptions] custom onConnect #104

AndreMaz opened this issue Dec 27, 2021 · 4 comments · Fixed by #105

Comments

@AndreMaz
Copy link
Member

AndreMaz commented Dec 27, 2021

I think I found a small issue when passing a custom onConnect middleware. The default onConnect looks like this

subscriptions: {
onConnect: (connectionParams, socket) =>
this.actions.ws({ connectionParams, socket }),
},

which simply calls the ws action to set the params

ws: {
timeout: 0,
visibility: "private",
tracing: {
tags: {
params: ["socket.upgradeReq.url"],
},
spanName: ctx => `UPGRADE ${ctx.params.socket.upgradeReq.url}`,
},
handler(ctx) {
const { socket, connectionParams } = ctx.params;
return {
$ctx: ctx,
$socket: socket,
$service: this,
$params: { body: connectionParams, query: socket.upgradeReq.query },
};
},
},

However, when passing custom onConnect I can't access the ws action because the this points to a SubscriptionServer instead of moleculer Service.

To solve it I've added the following lines in the created() function:

if (_.isFunction(mixinOptions.serverOptions.subscriptions.onConnect)){
    mixinOptions.serverOptions.subscriptions.onConnect = mixinOptions.serverOptions.subscriptions.onConnect.bind(this)
}

@Hugome can you please check if my approach is correct?

@icebob
Copy link
Member

icebob commented Dec 27, 2021

Maybe it's related #75

@AndreMaz
Copy link
Member Author

You're right, it's definitely related. @shawnmcknight do you have any thoughts on this?

@AndreMaz
Copy link
Member Author

Note: with this approach we continue to have access to the SubscriptionServer
image

@AndreMaz
Copy link
Member Author

AndreMaz commented Jan 8, 2022

@Hugome @shawnmcknight sorry to bother you again but do you have any thoughts about this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants