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() {
@Override
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.
@Override
public void onOpen(Session session, EndpointConfig endpointConfig) {
// sysout("Websocket connection established successfully");
this.userSession = session;
}
We are sending query like this to the Websocket,
@Override
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>
Topics help categorize Community content and increase your ability to discover relevant content.
Views
Replies
Total Likes
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:
Hope this helps or maybe someone has some real experience implementing WebSockets in AEM.
Good luck,
Daniel
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.
Regards,
Pabitra
Views
Replies
Total Likes
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
Views
Likes
Replies
Views
Likes
Replies