One of the most attractive features is JSF 2.3 added native web socket support, it means you can write real-time applications with JSF and no need extra effort.
To enable web socket support, you have to add javax.faces.ENABLE_WEBSOCKET_ENDPOINT in web.xml.
In the backing bean, inject a PushContext with @Push qualifier.
The first button sends a fixed hello string, and the second button accepts user custom message.
sendMessage and sendMessage2 will call sendPushMessage which utilizes the injected pushContext to send messages to the defined channel.
In the facelets template, onmessage indicates which handlers(onMessage method) will be used when messages is coming from the specified channel(helloChannel).
onMessage method will add the message body from web socket channel and put it into the content of message block.
Try click first button or input some messages and click the second button, the response message from server side will be displayed.
Scope
f:websocket has an attribute scope which accepts application, session, or view as its value, it is not difficult to understand.
Another attribute user can be used for identifying different client users, it can be a user id or serializable object that stands for a user. It is useful to communicate with a specific user.
Create a backing bean.
To demonstrate different cases, we defined a series of PushContext in the backing bean.
JSF 2.3 provides a WebsocketEvent, in the backend codes, you can observe it when it is opened (via CDI @Opened qualifier) or closed(via CDI @Closed qualifier).
Besides onmessage attribute of f:websocket, it also provides other two attributes onopen and onclose to listen the web socket connection when it is opened or closed. connected allow you set it disconnected by default, and use JSF built-in jsf.push.open() to connect to server side manually.
And the IDE console, you can see the log from WebsocketObserver.
Ajax
f:websocket can be bridged with f:ajax event attribute, and allow you trigger ajax event from web socket.
And facelets template.
In the backend codes, use pushContext send the f:ajax event name as content.
Another alternative here, use h:commandScript(newly added in JSF 2.3) with f:websocket instead, setonmessage handler as the name attribute of h:commandScript.
In JSF internally, JSF expose a default endpoint(/javax.faces.push/channelName) to serve the web socket connections. You can protect it as other web resources.
Source codes
Grab the source codes from my GitHub account, and have a try.