Last update: Edit

1 Introduction

The Mendix Runtime supports registering custom web socket endpoints using the javax.websocket API.

All you need to do is to use the method Core.addWebSocketEndpoint(String path, Endpoint endpoint) to register an instance of javax.websocket.Endpoint to respond to web socket requests on the given path. The Session ID of the client can be obtained from the EndpointConfig given in the onOpen method of the Endpoint.

Below is an example of how to register a websocket in your Mendix app.

2 Example

A simple implementation of an endpoint is shown below.

import javax.websocket.CloseReason;
import javax.websocket.Endpoint;
import javax.websocket.EndpointConfig;
import javax.websocket.MessageHandler;
import javax.websocket.Session;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

import com.mendix.core.Core;

public class TestEndpoint extends Endpoint {
  Set<Session> sessions = new HashSet<>();

  public void onOpen(Session session, EndpointConfig config) {
    String sessionId = (String) config.getUserProperties().get("mxSessionId");
    ISession mxSession = Core.getSessionById(UUID.fromString(sessionId));
    String username = mxSession.getUserName();
    session.addMessageHandler(new MessageHandler.Whole<String>() {
      public void onMessage(String message) {
        if ("test message".equals(message)) {
          try {
            session.getBasicRemote().sendText("test response:" + username);
          } catch (IOException e) {

    try {
      session.getBasicRemote().sendText("socket opened");
    } catch (IOException e) {

  public void onClose(Session session, CloseReason closeReason) {
    System.out.println("Received onClose call with reason: " + closeReason);

If this endpoint is registered by calling Core.addWebSocketEndpoint("/my-endpoint", new websockets.TestEndpoint()); then the following functionality is available at ws://.../my-endpoint:

  • When a connection is established, the server will send the message socket opened
  • If the client sends the message test message, the server responds with test response: USERNAME and closes the web socket