Topics
Replies

calum2atkins
05 Feb 2025, 22:32 ( Updated at: 14 Feb 2025, 18:16 )

RE: RE: C_BOT cloud instance receiving data from websocket for placing order.

toolsche said: 

PanagiotisCharalampous said: 

Hi there,

Please note that the WebSocket server needs to use this port as well. Else no communication is possible.

Here’s a step-by-step guide to set up your WebSocket server to communicate with your cBot:

  1. Set up your WebSocket Server:
    • Ensure that your WebSocket server is configured to listen and send messages on port 25345.

Here is a simple example using Node.js with the ws library:
 

const WebSocket = require('ws');

const server = new WebSocket.Server({ port: 25345 });

server.on('connection', socket => {
    console.log('Client connected');

    socket.on('message', message => {
        console.log('Received:', message);
        // Process the message and send a response if needed
        socket.send('Message received');
    });

    socket.on('close', () => {
        console.log('Client disconnected');
    });
});

console.log('WebSocket server is listening on port 25345');

  1. Update Your cBot to Connect to the WebSocket Server:
    • Ensure your cBot is configured to connect to your WebSocket server. The URI should look like this: wss://example.com:25345/.

Here is an example cBot code snippet:

using System;
using System.Text;
using cAlgo.API;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class WebSocketBot : Robot
    {
        private WebSocketClient _webSocketClient;

        protected override void OnStart()
        {
            _webSocketClient = new WebSocketClient();


            var uri = new Uri("wss://example.com:25345/");
            _webSocketClient.Connect(uri);

            Print("WebSocket connection opened");
        }


    }
}
  1. Testing:
    • Start your WebSocket server and ensure it is running and accessible on port 25345.
    • Deploy your cBot to the cTrader Cloud instance.
    • Monitor the logs to ensure the cBot is connecting to the WebSocket server and receiving messages correctly.

By following these steps, your cBot should be able to communicate with your WebSocket server, even in a cTrader Cloud instance.

If you encounter any issues or have further questions, please feel free to reach out.


 

Thank you. Works like a charm. :)

Hi, For this instance does this send any transactions placed? I'm looking to connect all transactions into my database, but always have the server disconnecting instantly after a message is sent

const WebSocket = require('ws');

const ws = new WebSocket('wss://demo.ctrader.com:5035/');

ws.on('open', function open() {
   console.log('✅ Connected to cTrader WebSocket');

   // Send a ping request
   ws.send(JSON.stringify({
       method: "ProtoOAPingReq",
       clientMsgId: "2"
   }));
});

ws.on('message', function incoming(data) {
   console.log('📩 Received:', data.toString());
});

ws.on('close', function close(code, reason) {
   console.log(`❌ WebSocket closed (Code: ${code}, Reason: ${reason})`);
});

ws.on('error', function error(err) {
   console.error('❌ WebSocket Error:', err);
});
 


@calum2atkins