PrimeFaces push with AWS

2019-07-23 03:56发布

问题:

I am using PrimeFaces 5.0 and tomcat 7 and my application is in AWS. I am trying to use PrimeFaces push. I have implemented a sample application and it is working with my local system. If i deploy the code to AWS it fails.

Xhtml code

<p:socket onMessage="jsFunctionToCall" channel="/service/push"/>

Web.xml

 <filter>
    <filter-name>shiroFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <init-param>
      <param-name>targetFilterLifecycle</param-name>
      <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>org.atmosphere.cpr.AtmosphereInterceptor</param-name>
        <param-value>org.atmosphere.interceptor.ShiroInterceptor</param-value>
      </init-param>
  </filter>
  <filter-mapping>
    <filter-name>shiroFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <servlet>
        <servlet-name>Push Servlet</servlet-name>
        <servlet-class>org.primefaces.push.PushServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>
    <servlet-mapping>
        <servlet-name>Push Servlet</servlet-name>
        <url-pattern>/primepush/*</url-pattern>
    </servlet-mapping>

Java code

@PushEndpoint("/service/push")
public class ServicePushResource {
    @OnMessage(encoders = {JSONEncoder.class})
    public String onMessage(String count) {
        return count;
    }
}

Pushing message:

EventBus eventBus = EventBusFactory.getDefault().eventBus();
eventBus.publish("/service/push", String.valueOf("msg"));

I am getting the error message that

WebSocket connection to 'ws://example.com/primepush/service/push?X-Atmosphere-tracking-id=0&X-Atmosphere-Framework=2.2.4-javascript&X-Atmosphere-Transport=websocket&X-Atmosphere-TrackMessageSize=true&X-atmo-protocol=true' failed: Error during WebSocket handshake: Unexpected response code: 501

After this error there is another error message that

Websocket failed. Downgrading to Comet and resending push.js.xhtml?ln=primefaces&v=5.1:1 Failed to load resource: the server responded with a status of 501 (Not Implemented) http://example.com/primepush/service/push?X-Atmos…ng&X-Atmosphere-TrackMessageSize=true&X-atmo-protocol=true&_=1422008343786

On inspecting both the error message, I found that the p:socket component is failing with transport as 'WebSocket' and it is trying with 'long-polling' which is not implemented. So my question is whether AWS support 'WebSocket' if not what is the workaround for that?

回答1:

Currently WebSocket is not supported with AWS. The work aroud is to use AWS EB loadbalancer with TCP/SSL. I did this by changing the protocol from HTTP to TCP in loadbalancer configuration. But this won't work alone, need to tell beanstalk to communicate directly to tomcat in case of websocket. This can be done using a configuration file which can be added as specified in this post.



回答2:

Documentation of Primefaces 5.0 says to use Atmosphere 2.1.3. I had had similar problem as you when I tried to use 2.2.x version. The current version I use is 2.1.4 and it works for me fine. Maybe you could try this also.

The other case is that firefox browser throws wss connection error (but primepush works anyway). Please refer also to: https://bugzilla.mozilla.org/show_bug.cgi?id=594502