Websocket connection with rails 7 to external wss return only ping

197 Views Asked by At

I try to connect to a websocket server from my rails project and send a graphql susbcription query. I tried with different methods , but still have the same problem : I receive a welcome message and then only infinite pings. I used gem "faye-websocket" or "websocket-client-simple" or "action_cable_client" , but same result. I also tried with .js solutions from a channel file too.

FOr my external websocket example i use Sorare api: https://github.com/sorare/api#subscribing-to-graphql-events

Do you have an idea , where is the problem ?

Here is a example with the gem "action_cable_client" https://github.com/NullVoxPopuli/action_cable_client

require 'json'
require 'action_cable_client'

EventMachine.run do
  uri = "wss://ws.sorare.com/cable"
  params = { channel: 'GraphqlChannel' }
  header = { "Authorization" => "Bearer #{ENV['S_TKN']}" }
  client = ActionCableClient.new(uri, params, true, header)

  query = <<-GRAPHQL
  subscription {
    aCardWasUpdated(rarities: [rare, super_rare, unique]) {
      slug
    }
  }
  GRAPHQL

  data = { query: query, variables: {}, action: 'execute' }
  message = { command: 'subscribe', identifier: {channel: 'GraphqlChannel'}, data: data }.to_json

  # called whenever a welcome message is received from the server
  client.connected { puts 'WebSocket connection successful.' }
 
  # Sends a message to the sever, with the 'action', 'speak'
  client.perform('speak', { message: message })

  # called whenever a message is received from the server
  client.received do | message |
    puts message
  end

  client.pinged do |_data|
    puts "ping"
  end

In case it's relevant, here is my config file:

  config.action_cable.disable_request_forgery_protection = true
  config.action_cable.url = 'wss://ws.sorare.com/cable'
  config.action_cable.allowed_request_origins = [ 'wss://ws.sorare.com/cable' , %r{http://sorare.*} ]
0

There are 0 best solutions below