Search on blog

Petition by Java EE Guardians

Twitter

luni, 4 aprilie 2016

OmniFaces and WebSockets Quickstart

Starting with OmniFaces 2.3 we can take advantage of a brand new feature - register a web socket push connection in client side. Thanks to the OmniFaces team this feature is available in today OmniFaces 2.3-RC1 via <o:socket/> tag.In this post, let's see a minimal usage of <o:socket/> tag.

First, ensure that you project contains the OmniFaces 2.3 dependency. Afterwards, in the JSF page, we need to add the<o:socket/>tag with its two required attributes:

·channel - This is javax.el.ValueExpression that must be evaluated to String and it represents the name of the web socket channel. A channel name is restricted to alphanumeric characters, hyphens, underscores and periods. A channel can have multiple open web sockets, and each of these sockets will receive the same push notification from the server.

·onmessage - This is javax.el.ValueExpression that must be evaluated to String and it represents the a JavaScript listener function that is automatically invoked when a push notification is received from the server.

The signature of the listener function for onmessage is of type:

function fooListener(message, channel, event) {

// message - the message pushed by the server

// channel - the channel name

// event - the raw MessageEvent instance

}

So, a simple <o:socket/> tag usage will look like this:

<o:socket channel="clock" onmessage="socketListener" />

<div id="clockId"></div>

<script type="text/javascript">

function socketListener(message, channel, event) {

document.getElementById("clockId").innerHTML += message + "<br/>";

}

</script>

By default, when we start the application, the web socket is automatically connected and open. As long as the document is open the web socket is open. When the document is unloaded the web socket is automatically closed. In the web socket is initially successfully connected but the connection is closed as a result of e.g. a network error or server restart, JSF will try to auto-reconnect it at increasing intervals.

Now, let's focus on the server side. Here we have to take into account the push messages mechanism. This mechanism is based on org.omnifaces.cdi.PushContext interface and org.omnifaces.cdi.Push API.

First, you need to know that by default the web socket is application scoped. This means that the managed bean that can push messages to this web socket must be in application scope (annotated with @ApplicationScope). In this case, the push message can be sent by all users and the application itself.

Furthermore, you have to inject PushContext via @Push annotation on the given channel name in any CDI/container managed artifact. For example:

@Inject

@Push(channel = "clock")

private PushContext push;

Finally, we need to write an action method capable to push messages to web socket via PushContext. For example:

Abonaţi-vă la JSF/OmniFaces Fans

Visitors Starting 4 September 2015

Others

[OmniFaces utilities]

Renderers

Collection of utility methods for the JSF API with respect to working with Renderer

Platform

This class provides access to (Java EE 6) platform services from the view point of JSF

Map Wrapper

Implementation of Map that wraps another map. This allows interception of one or more method on this wrapped map

JNDI

Utility class for simplifying some web related tasks that use JNDI under the hood, such as getting the <env-entry> from web.xml

State

Helper class for StateHelper that uses generic type-inference to make code that uses the StateHelper slightly less verbose

Beans

Collection of utility methods for the CDI API that are mainly shortcuts for obtaining stuff from the BeanManager

Components

Collection of utility methods for the JSF API with respect to working with UIComponent. There are several traversal/lookup methods, there are several UIForm and UIInput related methods which makes it easier to deal with forms and inputs

.Events

Collection of utility methods for the JSF API with respect to working with system and phase events