Using WebSocket in 1C

Alexander Biryukov

24.04.2025 12 min

Pic1.png

Let’s imagine this scenario: you're a 1C developer, you're earning a decent income, and you invest everything you make. And since you're also a geek (which pretty much comes with being a 1C developer), you're putting all your money into cryptocurrency.

Naturally, a very real question comes up: how do you keep an eye on crypto prices as they change so you can sell at a peak or buy during a dip?

Sure, you could rely on third-party tools. There are plenty of those already. But let’s face it, we’re geeks. Using someone else’s utilities doesn’t appeal to us. We’d rather create our own.

Let’s say we’re watching price data on the bitfinex.com exchange. We want to build an application that pulls data from this exchange and displays it in a convenient format. And since we're 1C developers, we want to build it using the 1C:Enterprise platform.

This setup is actually very convenient. You can keep track of the prices while staying focused on your main tasks.

So what’s the first thing that comes to mind when you want to retrieve data from a resource? The logical question is whether the resource offers a REST API.

We visit their portal, and at https://docs.bitfinex.com/docs/rest-general, we see that yes, it’s possible to connect and exchange data using REST.

That might sound like the solution. However, there is an important detail to consider.

Using the REST interface in this case, where you want a constant stream of quote updates, is not very efficient. You would need to write code that contacts the server at regular intervals and fetches new data. This would increase the load on both your system and the server. The more often you want updates, the more requests you send, increasing the load.

So what’s the alternative? Is there a better way? Yes, there is. If we check https://docs.bitfinex.com/docs/ws-general, we’ll find that in addition to REST, the portal also supports WebSocket communication. With WebSockets, a channel opens between the client and server, and data flows through that channel continuously. You don’t need to send repeated requests or wait for responses. The entire process becomes much simpler.

Although WebSockets have clear advantages, the 1C:Enterprise language didn’t support them until recently. The platform simply lacked the capability. But in version 8.3.27, WebSocket support was introduced. Now we have a chance to explore this new feature and at the same time build a small tool for monitoring crypto quotes.

First, create a new 1C infobase. Open it in Designer. Create a common form.

Open the form and add two commands: Open Socket and Send Message to Socket.

The 1C documentation includes enough material on WebSockets to help you get started.

Now let’s write the code to open a WebSocket: 

&AtClient

Procedure cmdOpenSocket(Command)



          WebSocketHandlers = New WebSocketClientConnectionHandlers;

              

          WebSocketHandlers.Module                       = ThisObject;

          WebSocketHandlers.OnOpenHandler        = "WebSocket_OnOpenHandler";

          WebSocketHandlers.OnCloseHandler        = "WebSocket_OnCloseHandler";

          WebSocketHandlers.OnMessageHandler  = "WebSocket_OnMessageHandler";

          WebSocketHandlers.OnErrorHandler         = "WebSocket_OnErrorHandler";

              

          WebSocketClientConnections.OpenConnection("bitfinex-pub", "wss://api-pub.bitfinex.com/ws/2", WebSocketHandlers);

              

EndProcedure

 

&AtClient

         Procedure WebSocket_OnOpenHandler(WebSocketConnection) Export

EndProcedure

 

&AtClient

Procedure WebSocket_OnMessageHandler(WebSocketConnection, WebSocketMessage) Export

         Message(WebSocketMessage);

EndProcedure

 

&AtClient

Procedure WebSocket_OnErrorHandler(WebSocketConnection, ErrorCode, ErrorDescription) Export

         Message("webSocket error: " + ErrorCode + ", " + ErrorDescription);

EndProcedure

 

&AtClient

          Procedure WebSocket_OnCloseHandler(WebSocketConnection, CloseCode) Export

EndProcedure

This code is very straightforward. First, we create the WebSocketHandlers object and then set its properties. These include the module where the event handlers are located and the names of the procedures that will respond to each event.

In this example, the handlers are all located in the same module, so we use ThisObject.

There’s not much else to say about the handlers. They are also simple.

Now that we’ve opened a WebSocket connection, we can send a message through it. Here’s the code for the Send Message to WebSocket command:

&AtClient

Procedure cmdSendMessageToSocket(Command)

              

       // sending data to WebSocket

      textJSON = "{""event"":""subscribe"",""channel"":""ticker"",""symbol"":""tBTCUSD""}";

              

       WebSocket = WebSocketClientConnections.GetConnection("bitfinex-pub");

       WebSocket.SendMessage(textJSON);

              

EndProcedure

This part is also simple. We send a message using the connection we opened earlier. The message is a JSON string.

If the WebSocket sends something back, the WebSocket_OnMessageHandler procedure will process it and display it in the 1C message bar.

There’s one last detail. To make sure our common form opens automatically when 1C:Enterprise launches, let’s add it to the Home Page.

Now let’s see how everything behaves. Start 1C in dialog mode. Our form opens right away:

Click Open Socket. If everything is configured correctly and no errors occur, the WebSocket will return some service data. This will immediately appear in the message window:

It looks like everything is functioning as expected

Now let’s send a message to the open WebSocket. Click Send Message... and you’ll see the data start flowing into 1C from the exchange:

Notice that the data arrives on its own. 1C is not sending any requests to the server. The open WebSocket connection keeps everything streaming automatically. The incoming data is raw. It will need to be cleaned up before further use, but the main idea should be clear.

So now we’ve seen that the 1C:Enterprise platform includes a new object: WebSocket. We’ve also walked through a simple example that shows how it can be applied in practice.

Stay tuned. Choose reliable cryptocurrencies for your investments, and you’ll be in a good place

As always, the example used in this article is included.

Be the first to know tips & tricks on business application development!

A confirmation e-mail has been sent to the e-mail address you provided .

Click the link in the e-mail to confirm and activate the subscription.