I cannot get GraphQL subscriptions running. Queries do work (e.g. messages), the only available subscriptions (chatMessageAdded) gives neither error feedback or informs about new messages in a channel.
My goal is to get live updates of messages of a certain channel, where the user is not part of. In a perfect world, the user may be anonymous - but I guess, that'a whole other story.
In my example, consider it a list of several 'small room previews' (read-only) rather than an actual 'chat frontend'.
messages is used to fetch the latest x messageschatMessageAdded subscription is established for this room in order to get informed about new messages. I tried to debug this as much as I can. Could you give advice on what could be wrong?
In order to achieve this goal, I see GraphQL subscriptions as the only suitable way. Why?
I tried to inspect & debug the entire process, here's the outcome:
chatMessageAdded subscription is located herePubSub in this context holds the subscription in-memory (see graphql-subscriptions)afterSaveMessage is executed correctlypublishMessage gets executed with passing the correct message object to PubSubpubsub.asyncIterator will not be triggered, thus none of the actual subscription code gets executedpubsub.subscriptions is empty -> {}Just for the sake of completeness, the two GraphQL documents I use as example:
const listMessagesDocument = gql`
query listMessages {
messages(channelId: "DveDZbQDQWcT7wPRd", count: 5) {
messagesArray {
author {
username
}
content
id
}
}
}
`;
const messageAdded = gql`
subscription {
chatMessageAdded(channelId: "DveDZbQDQWcT7wPRd") {
content
}
}
`;
WebSocket frames
{"type":"connection_init","payload":{}}
{"type":"connection_ack"}
{"id":"1","type":"start","payload":{"variables":{},"extensions":{},"operationName":null,"query":"subscription {\n chatMessageAdded(channelId: \"DveDZbQDQWcT7wPRd\") {\n content\n __typename\n }\n}\n"}}
Rocket.Chat Server logs
Webserver logs
@winterstefan first of all, thanks for the very detailed explanation of the problem. I tested the situation that you are reporting, and I found the problem. Basically the problem is with authentication, the authenticated function does not receive the token, because the connection is different from the normal that you send the authToken. As we can see here, the websocket connection expect a parameter that contain the authToken, but the problem is that we CAN'T sent headers over websocket connection, and the subscriptions-transport-ws from apollo-graphql can't handle with this, as you can see in this doc. There is no correct way to authenticate a connection over websocket and graphql(at least until now). Apollo graphql says that we should use the apollo-client to be able to set this connectionParams with the expected authToken.
See apollo explanation: https://www.apollographql.com/docs/graphql-subscriptions/authentication.html
and possible solution to this: https://www.apollographql.com/docs/react/advanced/subscriptions.html#authentication
Please let me know if I can help you with this.
@MarcosSpessatto Thanks for the explanation!
I'm not sure whether I fully understood it, but I played around a bit after reading your answer and the associated docs.
By just passing the token as connectionParams.Authorization, I managed to get my use-case running:
new WebSocketLink({
uri: 'myuri.example.com',
options: {
reconnect: true,
connectionParams: {
Authorization: 'MY-TOKEN-HERE',
},
},
...config,
});
With that, the token gets sent in the initial connect frame of the WebSocket.
Authorization param was handled correctlySo for me, I'd consider it working. Is there anything I left out / that's wrong with my solution? Otherwise, I would mark this issue als solved.
@winterstefan Your solution looks good! Unfortunately is the only way to send the token when the socket connects.
Okay, thank you very much for your help!
_For me, the case is solved -> closing issue_
Hi @winterstefan,
We are planning to remove the support for GrapgQL on the next release and work a new implementation later.
Are you using the GraphQL API in production?
Can you please give us your opinion on the issue?
@engelgabriel Thank you very much for mention + hint about GraphQL. See my latest comment with #14959 馃憤