JSF WebSocket with OmniFaces in Oracle ADF

Hi,

In previous post I shared another example of usage of WebSocket technology within Oracle ADF 12cR2.


ADF 12cR2: Using WebSockets for warning Task Flow Concurrency 

As part of my presentation in NLOUG 2018 I compared JSF 2.3 declarative way of doing WebSocket vs Oracle ADF (JSF 2.2) way.

I started being using OmniFaces as a very good Faces utility for my JSF projects and seen <o:socket...> which functionality is "the same" as <f:websocket...> for the implementation of WebScoket in JSF in a declarative way.

By using OmniFaces you can extend the possibilities of Oracle ADF 12cR2 with some useful components and libraries.

The example can be found in my GitHub of ADF Demos: https://github.com/DanielMerchan/adf

In this simple example I made use of the <o:socket...> for WebSocket implementation.
But, you can think in more complex scenarios where:
  • You want to communicate with a certain set of users
  • You can do Client - Server and viceversa communication through the WebSocket.
OmniFaces-Socket Documentation
How can I use OmniFaces in Oracle ADF 12cR2? Easy.
  • Add to your Maven the latest OmniFaces 2.x dependency. Remember that Oracle ADF 12cR2 works over JSF 2.2 and OmniFaces 3.x is compatible with JSF 2.3!

    
                org.omnifaces
                omnifaces
                2.7
                jar
                compile
            
  • Start using API or its tags.

    <?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE html>
    <f:view xmlns:f="http://java.sun.com/jsf/core" xmlns:af="http://xmlns.oracle.com/adf/faces/rich"
    xmlns:o="http://omnifaces.org/ui">
        <af:document title="testOmniWebSocket.jsf" id="d1">
            <af:form id="f1">
                <af:panelHeader text="Usage of OmniFaces WebSocket as ADF (JSF 2.2) cannot use JSF 2.3 WebSocket" id="ph1">
                    <o:socket channel="gooseChannel" onmessage="gooseListener"/>
                </af:panelHeader>
                <af:button text="Test Omni Socket Push" actionListener="#{socketBean.sendMessage()}" id="b1"/>
                <af:resource type="javascript">
                    function gooseListener(message, channel, event) {
                        $.notify("Message: " + message,{position:"top center",className:"warn"});
                    }
                </af:resource>
            </af:form>
            <f:facet name="metaContainer">
                <af:resource type="javascript" source="/resources/js/jquery-3.3.1.min.js"/>
                <af:resource type="javascript" source="/resources/js/notify.min.js"/>
             </f:facet>
        </af:document>
    </f:view>
  • Push the notification from Server Side by using the PushContext of OmniFaces

    package com.magicpigeon.poc.view;
    
    import java.time.LocalDateTime;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.enterprise.context.RequestScoped;
    
    import javax.inject.Inject;
    import javax.inject.Named;
    
    import org.omnifaces.cdi.Push;
    import org.omnifaces.cdi.PushContext;
    
    /**
     * Demo Purpose Managed Bean for illustrating the PushContext WebSocket using OmniFaces
     * @author Daniel Merchan Garcia
     */
    @RequestScoped
    @Named("socketBean")
    public class SocketBean {
        
        @Inject @Push(channel="gooseChannel")
        private PushContext gooseChannel;
    
        /**
         * Default Constructor
         */
        public SocketBean() {
            super();
            System.out.println("aksjhdiahsdaiu");
        }
        
        public void sendMessage() {
            gooseChannel.send("Hello " + " at " + LocalDateTime.now());
        }
    }
    

Comments

Popular posts from this blog

OJET: Inter-Module communication in TypeScript Template

OJET: Build and Deploy in an Application Server

OJET: Select All options using only Checkboxset