WAQAR SHAIKH
WAQAR SHAIKH

Reputation: 141

Forex.com streaming (lightstreaming) API integration in Golang

I have been trying to integrate forex.com Trading API and streaming API with Golang.

I was able to integrate their Trading API but i am still struggling with their streaming API.

They don't have well documented stuff but it is all available at https://docs.labs.gaincapital.com/index.html#Getting%20Started/CIAPI%20Arky.htm?TocPath=Getting%2520Started%257C_____1

At present, I am facing a problem connecting to WebSocket for connection before listening to streams.

Facing error as:

no route to host

Code link at https://go.dev/play/p/5xRDE8l5b41

I have not added appkey as it will be required after getting a valid connection but I am not getting that.

package main

import (
"fmt"
"log"
"net/url"

"golang.org/x/net/websocket"
)

const (
websocketURL = "wss://push.cityindex.com/"
appKey       = "YOUR_APP_KEY" // replace with your actual AppKey
marketID     = "402044079"    // example for German 40 index
)

func main() {
u := url.URL{Scheme: "wss", Host: "push.cityindex.com", Path: "/"}
ws, err := websocket.Dial(u.String(), "", "https://push.cityindex.com/")
if err != nil {
log.Fatalf("Failed to connect to WebSocket server: %v", err)
}
defer ws.Close()

// Authenticate using the AppKey
authMessage := fmt.Sprintf("CONNECT\nappKey=%s\n", appKey)
if _, err := ws.Write([]byte(authMessage)); err != nil {
log.Fatalf("Failed to send auth message: %v", err)
}
log.Println("Sent authentication request")

// Subscribe to the specified market
subscribeMessage := fmt.Sprintf("SUBSCRIBE\nitem=%s\nschema=MERGE\nmode=MERGE", marketID)
if _, err := ws.Write([]byte(subscribeMessage)); err != nil {
log.Fatalf("Failed to send subscribe message: %v", err)
}
log.Printf("Subscribed to market ID: %s", marketID)

// Read and print streaming data
for {
var message = make([]byte, 512)
n, err := ws.Read(message)
if err != nil {
log.Println("Read error:", err)
break
}
log.Printf("Received data: %s", message[:n])
}
}

How can I resolve this?

Upvotes: 0

Views: 34

Answers (0)

Related Questions