PubSub Twitch API - Connection with websocket

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Dahaarl

Hi !

I’m trying to use the twitch PubSub API with websocket connection and I can’t manage to receive the basic PONG response after sending a PING

Here’s the log I got after running the code:

> Connecting...
> Socket Open with 
> SENT {"type":"PING"}
> Socket Closed, clean : False
--- Debugging process stopped ---

I don’t know if I’m sending the {"type" : "PING"} the right way, any ideas ?

Here’s my code with a fake client_id that correspond to my App’s token registered inside the twitch dev dashboard.

extends Node


var client_ID := "helloworld123456789"
var socket := WebSocketClient.new()


# Called when the node enters the scene tree for the first time.
func _ready():
	socket.connect("connection_closed", self, "_on_connection_closed")
	socket.connect("connection_error", self, "_on_connection_error")
	socket.connect("connection_established", self, "_on_connected")
	socket.connect("data_received", self, "_on_data")
	socket.connect("server_close_request", self, "_on_ask_to_close")
	 
	socket.verify_ssl = true
	socket.get_peer(1).set_write_mode(WebSocketPeer.WRITE_MODE_TEXT)
	
	var err = socket.connect_to_url("wss://pubsub-edge.twitch.tv")
	
	if err == OK:
		Logger.log("Connecting...")
		set_process(true)
	elif err != OK:
		Logger.log("Error Connecting")
		set_process(false)
		

func _process(delta):
	if(socket.get_connection_status() != NetworkedMultiplayerPeer.CONNECTION_DISCONNECTED):
		socket.poll()


# SIGNALS
func _on_connection_closed(was_clean := false):
	Logger.log("Socket Closed, clean : " + str(was_clean))
	set_process(false)

func _on_connection_error():
	Logger.log("Socket Error.")

func _on_connected(protocol := ""):
	Logger.log("Socket Open with " + protocol)
	send_data({"type" : "PING"})
	
func _on_ask_to_close(code: int, reason: String):
	Logger.log("Ask to close connection. " + str(code) + " " + reason)


# DATA EXCHANGE FUNC 
func send_data(command):
	var cmd_to_send = JSON.print(command)
	Logger.log("SENT " + cmd_to_send)
	var x = socket.get_peer(1).put_packet(cmd_to_send.to_utf8())
	
func _on_data():
	var payload = JSON.parse(socket.get_peer(1).get_packet().get_string_from_utf8()).result
	Logger.log("DATA RECEIVED " + str(payload))