Open jlouvel opened 10 years ago
References:
A Sock.js-compatible module would be nice : https://github.com/sockjs Also, maybe add support for STOMP to format websocket messages ?
Good ideas, thanks Olivier.
Here is a pointer to STOMP http://jmesnil.net/stomp-websocket/doc/
Has this idea been forfeited? I'm looking for an extension to our Restlet backend that would allow server to send events to the client rather than doing this via polling.
A workaround I did for this is to create a separate WebSocket servlet in the same web app, the downside is I can't use Restlet resources, so yes, it would be great if Restlet can natively support WebSocket.
@cyberquarks can you show some gists with the configuration you mentioned? I tried implementing this myself but couldn't find the right place to start back then. I would love to see some example :-)
@lukaszbachman it's simple actually, just make a Websocket servlet separate to Restlet, since Restlet essentially is also just a servlet, the way you can utilize the WebSocket from Restlet is actually to have a "subscription" from the Restlet servlet to the Websocket servlet.
@cyberquarks I'm using Restlet J2SE as a standalone server running on an embedded jetty connector (org.restlet.ext.jetty
). I vaguely remember that this is exactly what I had problems with - using the Restlet's API to add a regular servlet. I guess you are doing this differently and you use other servlet container (like Tomcat) and deploy your Restlet webapp there?
@lukaszbachman pardon my long overdued response, what I actually did was to use Atmosphere Framework and plug it into my Restlet web application, but something like this should work:
<servlet>
<servlet-name>RestletServlet</servlet-name>
<servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
<init-param>
<param-name>org.restlet.application</param-name>
<param-value>com.myapp.MyApplication</param-value>
</init-param>
</servlet>
<!-- Catch all requests -->
<servlet-mapping>
<servlet-name>RestletServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet>
<description>WebsocketServlet</description>
<servlet-name>WebsocketServlet</servlet-name>
<servlet-class>...</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AtmosphereServlet</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
What's happening essentially here is you're just mapping a separate servlet (Websocket in this case) along with your web application. And in order for your Restlet application to interact with the Websocket it can listen to the Websocket like this (somewhere in your Restlet mapped service):
WebSocketClient client = new WebSocketClient( new URI( "ws://ws" ), new Draft_10() );
client.connect();
JSONObject obj = new JSONObject();
obj.put("foo", "bar");
String message = obj.toString();
client.send(message);
At this point, Restlet is able to "interact" with the Websocket server but only one way, I have not figured a way for Restlet to wait and receive messages from websocket, maybe a blocking resource or something. @jlouvel maybe can comment on this.
@cyberquarks thanks for your reply! Much appreciated you took the time. Unfortunately, we are using Restlet in a standalone mode with an embedded jetty server, so we don't have direct access to servlets API. So unless Restlet makes it somehow possible through their own API, we are back to the square one :-/
Suggested by Olivier Croisier
workload: 2d (seems well documented, perhaps have a first step of analysis, before estimating the workload)