Injecting Custom Data In Websocket Client Headers

Customizing WebSocket client headers lets you inject specific information, preferences, or requirements into the WebSocket connection establishment process.

From 4D v20 R3, you can transmit security information, application-specific data, or other data relevant to the server connection through websocket client headers.

Here is everything you need to know!

As described in the Websocket client blog post, you must create a class to manage the websocket events.

This class supports a new attribute, headers, which is an object where the attributes have this syntax: headers.key:=value.

For example, if you want to transmit an authorization token to the server, you need to create a class we’ll call WSConnectionHandler class :

Class constructor($myToken:Text)

// Creation of the headers sent to the server
This.headers:=New object ("x-authorization";$myToken)
// Cookie header should use this syntax:
This.headers.Cookie:="yummy_cookie=choco; tasty_cookie=strawberry"

Function onMessage($ws : Object; $event : Object)

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

Check out the documentation for more details! And feel free to join the discussion on the 4D Forums.

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.