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

How do I implement custom messages, just like websockets #633

Open
723386252 opened this issue Nov 15, 2023 · 2 comments
Open

How do I implement custom messages, just like websockets #633

723386252 opened this issue Nov 15, 2023 · 2 comments

Comments

@723386252
Copy link

723386252 commented Nov 15, 2023

I have a requirement that the client only sends data to the server, and the server can freely choose who to push messages to, just like a native websocket. The purpose of doing so is to send authentication information to the server, and the server can return the permissions I have, such as read-only. I know this can be achieved using websocket, but I hope to directly access agent.custom during the authentication process and bind the permission information to it.

@alecgibson
Copy link
Collaborator

We're slightly hacking our way around this issue by piggy-backing the handshake message.

So on the server, we have some middleware:

backend.use('reply', ({reply, agent}) => {
  if (reply.a !== MESSAGE_ACTIONS.handshake) return;
  const auth = agent.custom;
  reply.$custom = {auth};
});

And then on the client, you can parse it out:

connection.on('receive', ({data}) => {
  if (data.a !== MESSAGE_ACTIONS.handshake) return;
  const {auth} = data.$custom;
  // do stuff with auth
});

@723386252
Copy link
Author

We're slightly hacking our way around this issue by piggy-backing the handshake message.

So on the server, we have some middleware:

backend.use('reply', ({reply, agent}) => {
  if (reply.a !== MESSAGE_ACTIONS.handshake) return;
  const auth = agent.custom;
  reply.$custom = {auth};
});

And then on the client, you can parse it out:

connection.on('receive', ({data}) => {
  if (data.a !== MESSAGE_ACTIONS.handshake) return;
  const {auth} = data.$custom;
  // do stuff with auth
});

Thank you for your reply, but there is still one issue. The client cannot independently request the server. In other words, I can only rely on other requests to attach relevant information, which is very inconvenient. I hope to be able to independently request the server like websocket, and the server can also push my customized information to the client separately

I have come up with a good way to bypass it, using presence to simulate API requests

const presence = connection.getPresence('api:auth')
const localPresence = presence.create(id)
localPresence.submit(data, result => {
 
})

and in server,I intercepted the presence at the beginning of the API and used callback to return it in an incorrect form

backend.use('receivePresence', (context, next) => {
  const {ch} = context
  if (/^api:/.test(ch)) {
    // do something
    return next(result)
  }
})

At this point, I can achieve functions similar to APIs, but the server actively pushes customized messages to the client, which I am still unable to achieve

A more compromise approach is to obtain the websocket instance in the context and use websocket for push

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

No branches or pull requests

2 participants