4D Blog

Home Product Websocket Client

Websocket Client

July 17, 2023

Product

In today’s interconnected world, the need for real-time updates is paramount for websites and IoT applications.

A way to deliver information instantly to your sites or applications is to use the Websocket protocol which provides a full-duplex communication channel between a server and a client. From 4D V20, you can create a websocket server with the 4D.WebSocketServer class. We continue in v20 R2 with the 4D.WebSocket class that allows you to create a websocket client to connect to any websocket servers.

HDI websocket

In this blog post, we will connect to the chat server given as an example in the websocket server blog:

In this example, messages will be displayed in a listbox populated by the Form.messages collection.

manage websocket events

First, you need to create a class to manage the websocket events and instantiate it. To do so, you need to create a class with some of the following functions, depending on your needs:

  • onMessage(), called each time a message arrives through this connection,
  • onOpen(), called when the websocket connection is opened,
  • onTerminate(), called when the websocket connection is closed,
  • onError(), called when an error occurred.

 

In our example, we want to display the message received from the server and display the connection closing. So we create this WSConnectionHandler class:

Class constructor
	
Function onMessage($ws : Object; $event : Object)
   Form.messages.push($event.data)

Function onTerminate($ws : Object; $event : Object)
   Form.messages.push("Connection closed")
   

And we want to send messages written by the user to the server:

// Sends a message through the websocket client connected to the websocket server
Form.webSocket.send(Form.message)

connect to the server

To connect your application to the websocket server, you need to instantiate the 4D.WebSocket using the server url as the first parameter and your WebSocket connection handler in the second:

Form.webSocket:=4D.WebSocket.new($wssUrl; cs.WSConnectionHandler.new())

Check out this feature with the HDI and the documentation for more details!

Discuss

Tags 21, IoT, v20 R2, Websocket

Latest related posts

  • February 3, 2026

    4D Write Pro – Adding a margin automatically when bullets are set using standard actions

  • January 22, 2026

    Transform Static Documents into Actionable Knowledge with AIKit

  • January 22, 2026

    Deploy Fluent UI effortlessly in your 4D applications

Fabrice Mainguené
Fabrice Mainguené
• Product Owner •Fabrice Mainguené joined 4D Program team in November, 2016. As a Product Owner, he is in charge of writing the user stories then translating it to functional specifications. His role is also to make sure that the feature implementation delivered is meeting the customer need.After obtaining a Bachelor degree in Computer Science at CNAM, Fabrice joined a small software publishing company as a Windev developer. Then he worked for different companies in industry and trade areas as a Windev and web developer as well as technical advisor on new features.
  • Deutsch
  • Français
  • English
  • Português
  • Čeština
  • Español
  • Italiano
  • 日本語

Categories

Browse categories

  • AI
  • 4D View Pro
  • 4D Write Pro
  • 4D for Mobile
  • Email
  • Development Mode
  • 4D Language
  • ORDA
  • User Interface / GUI
  • Qodly Studio
  • Server
  • Maintenance
  • Deployment
  • 4D Tutorials
  • Generic
  • 4D Summit sessions and other online videos

Tags

4D AIKit 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R10 21 21 R2 Administration AI Artificial Intelligence Build application Class Client/Server Code editor Collections Compatibility settings Formula Google Listbox Logs Mail Network Objects OpenAI ORDA PDF Pictures Preemptive Programming REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience vscode Web Word processor

Tags

4D AIKit 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R10 21 21 R2 Administration AI Artificial Intelligence Build application Class Client/Server Code editor Collections Compatibility settings Formula Google Listbox Logs Mail Network Objects OpenAI ORDA PDF Pictures Preemptive Programming REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience vscode Web Word processor
Subscribe to 4D Newsletter

© 2026 4D SAS - All rights reserved
Terms & Conditions | Legal Notices | Data Policy | Cookie Policy | Contact us | Write for us


Subscribe to 4D Newsletter

* Your privacy is very important to us. Please click here to view our Policy

Contact us

Got a question, suggestion or just want to get in touch with the 4D bloggers? Drop us a line!

* Your privacy is very important to us. Please click here to view our Policy