Need to connect to Websocket using OSGI service to communicate with a third-party chatbot | Community
Skip to main content
pmanna
December 16, 2024

Need to connect to Websocket using OSGI service to communicate with a third-party chatbot

  • December 16, 2024
  • 3 replies
  • 1122 views

Hello members,

 

Requirement
We need to connect to Websocket to establish conversation with a third-party chatbot.

Challenges: We couldn't find any reference document on the community on establishing Websocket connection.

Possible Solution : We are using javax.websocket API to connect to Websocket. We have created one ClientEndPoint to create the Websocket connection, then we are sending the query to Websocket.

Problem: Although we are able to establish the Websocket connection, we are unable to receive messages back from Websocket. The "onMessage" method should be called by Websocket and the response should be captured.

Method to create Websocket connection:
public WebSocketClientEndpoint(URI endpointURI, String auth, String sessionId) {
try {
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
ClientEndpointConfig.Configurator configurator = new ClientEndpointConfig.Configurator() {
@9944223
public void beforeRequest(Map<String, List<String>> headers) {
// Adding required headers, passing the auth and Session ID
}
};
ClientEndpointConfig config = ClientEndpointConfig.Builder.create().configurator(configurator).build();
container.setDefaultMaxSessionIdleTimeout(120 * 60 * 1000);
container.connectToServer(this, config, endpointURI);
} catch (Exception e) {
// handle exception
}
}

On successful connection below method will be called.
@9944223
public void onOpen(Session session, EndpointConfig endpointConfig) {
// sysout("Websocket connection established successfully");
this.userSession = session;
}

We are sending query like this to the Websocket,
@9944223
public synchronized void sendBinaryMessage(byte[] message) throws IOException {
if (this.userSession.isOpen()) {
RemoteEndpoint.Basic basicRemote = this.userSession.getBasicRemote();
basicRemote.sendBinary(ByteBuffer.wrap(message));
}
}

We are not receiving the response back on below method,
@OnMessage
public void onMessage(byte[] message) {
}

Although the connection is established successfully, but we are not getting the response back on the "onMessage" method for some reason. We are also not getting any error response from the Websocket. Are we missing anything on the approach? Any guidance on this would be really helpful.

The two maven dependencies which we have used are below,

<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>9.0.41</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-websocket</artifactId>
<version>9.0.41</version>
</dependency>

3 replies

daniel-strmecki
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
December 16, 2024

Hi @pmanna,

I don't think you are missing anything in your code, but probably the response is blocked on the AEM side. Several OOTB security mechanisms might prevent your message from being received and processed by AEM. I suggest you start by looking at the request and error logs.

I never tried implementing WebSockets in AEM, but keep in mind that AEM is primarily designed for HTTP-based content delivery, and WebSocket support might require additional configurations.

Here are a few more suggestions you can check:

  • Use a tool like wscat or browser developer tools to test WebSocket connections
  • Check network and firewall configurations to allow WebSocket traffic on the required ports
  • Increase session timeout settings in AEM and any proxies or load balancers
  • Ensure your WebSocket servlet or implementation is properly registered in OSGi
  • Update the Dispatcher configuration to allow WebSocket protocols
    • Ensure the /clientheaders section in dispatcher.any includes headers like Upgrade and Connection
    • Ensure the servlet handles Upgrade and Connection headers to initiate WebSocket communication.

Hope this helps or maybe someone has some real experience implementing WebSockets in AEM.

 

Good luck,

Daniel

pmanna
pmannaAuthor
December 17, 2024

Hi Daniel,

 

Thanks for your suggestions. I will check each point and update on this. One more point, I have one simple Java application where I am trying the same thing and I am getting the response back from the Websocket. So, even I think that somewhere the messages are blocked. Currently, I am trying to integrate on my local server.

 

Few points on the checkpoints you suggested.

 

  • Use a tool like wscat or browser developer tools to test WebSocket connections - Yet to try this one.
  • Check network and firewall configurations to allow WebSocket traffic on the required ports - So, the Websocket starts with "wss://"  and it connection will happen on 443 port, I need to check that.
  • Increase session timeout settings in AEM and any proxies or load balancers - I tried with increasing the timeout, but that didn't help.
  • Ensure your WebSocket servlet or implementation is properly registered in OSGi - My bundle is active, javax.websocket is active, my calling servlet is also active.
  • Update the Dispatcher configuration to allow WebSocket protocols
    • Ensure the /clientheaders section in dispatcher.any includes headers like Upgrade and Connection - Will explore more on this.
    • Ensure the servlet handles Upgrade and Connection headers to initiate WebSocket communication. - Will explore more on this.

 

Regards,

Pabitra

 

arunpatidar
Community Advisor
Community Advisor
December 17, 2024
pmanna
pmannaAuthor
December 18, 2024

Hi Arun,

 

I checked this link before. The Netty approach I tried, I was getting lots of dependency errors. But I am able to connect to Websocket by using both Javax Websocket and Jetty. But the pattern of the error is same for both the cases. I am able to create the session, but not receiving any response from Websocket. However, for both the scenarios, I received error response from Websocket only once, after that Websocket never responded. The error we received from Websocket was related to some separate metadata related to our project. I am not sure, if I need to do any other configuration on OSGI console or not. I reached out to Adobe Support team, waiting for their response.

 

Regards,

Pabitra

kautuk_sahni
Community Manager
Community Manager
January 7, 2025

@pmanna Did you find the suggestion helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!

Kautuk Sahni