I am building a web app using elixir phoenix absinthe graphql for the backend. I have a messaging module which requires absinthe subscriptions. For some reason I am not able to get it to work. Here is what I have for the absinthe subscription.
subscription do
field :new_message, :message do
arg(:listing_id, non_null(:id))
arg(:receiver_id, non_null(:id))
trigger(
:create_message,
topic: fn
%Message{} = message -> ["new_message:#{message.receiver.id}:#{message.listing.id}"]
_ -> []
end
)
config(fn args, _info ->
{:ok, topic: "new_message:#{args.receiver_id}:#{args.listing_id}"}
end)
end
end
for this demo, the message model is simple. it has the following fields
schema "messages" do
field :deleted_at, :naive_datetime
field :read_at, :naive_datetime
field :body, :string
# belongs_to :contact, Contact, type: :binary_id
belongs_to :sender, User, foreign_key: :sender_id, type: :binary_id
belongs_to :receiver, User, foreign_key: :receiver_id, type: :binary_id
belongs_to :listing, Listing, foreign_key: :listing_id, type: :binary_id
timestamps(type: :utc_datetime)
end
The goal is to do the following.
- When a user sends a message, he should see the message instantly.
- It should also show up in the receivers message window immediately.
I am not able to get the above working… Am I doing anything wrong? How can I make this subscription work?