C_BOT cloud instance receiving data from websocket for placing order.

Created at 22 Jun 2024, 14:09
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
TR

tradingmaster34

Joined 29.06.2022

C_BOT cloud instance receiving data from websocket for placing order.
22 Jun 2024, 14:09


Hello guys, I ve build a cBot which place order from a WebSocket message. In local its working , but in a cloud instance its doesn't place any orders. After some research found that winapi and dll are not allowed in cloud instances. However the thing is there they mentioned that, cloud instance can receive message from WebSocket client when set up to a service port at 25345. How to achieve this? for example my websocket server is wss://example.com. Where should i configure the port id? please help. 

Thanks 


@tradingmaster34
Replies

PanagiotisCharalampous
26 Jun 2024, 07:43

Hi there,

Cloud execution does not allow access to the internet at the moment.

Best regards,

Panagiotis


@PanagiotisCharalampous

tradingmaster34
02 Jul 2024, 22:10 ( Updated at: 03 Jul 2024, 07:24 )

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

PanagiotisCharalampous said: 

Hi there,

Cloud execution does not allow access to the internet at the moment.

Best regards,

Panagiotis

But i ran into these, they have mentioned it'll support with correct port.


@tradingmaster34

PanagiotisCharalampous
03 Jul 2024, 08:41

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.


 


@PanagiotisCharalampous