Skip to content

Commit

Permalink
OmniFaces WebSocket demo (#490)
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware authored Nov 25, 2024
1 parent 9885982 commit 32b7baa
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,22 @@
package org.primefaces.showcase.view.ajax;

import jakarta.annotation.PostConstruct;
import jakarta.faces.push.Push;
import jakarta.faces.push.PushContext;
import jakarta.faces.view.ViewScoped;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import java.io.Serial;
import java.io.Serializable;

import io.quarkus.logging.Log;
import io.quarkus.runtime.annotations.RegisterForReflection;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.jbosslog.JBossLog;
import org.omnifaces.cdi.Push;
import org.omnifaces.cdi.PushContext;

@Named
@ViewScoped
@JBossLog
@RegisterForReflection(serialization = true)
public class WebSocketView implements Serializable {

Expand Down Expand Up @@ -73,7 +74,7 @@ public void init() {
}

public void sendMessage() {
Log.infof("Sending message: %d", count);
log.debugf("Sending message: %d", count);
pushChannel.send("quarkusMessage");
}

Expand Down
12 changes: 7 additions & 5 deletions src/main/resources/META-INF/resources/ui/ajax/websocket.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,29 @@
xmlns:ui="jakarta.faces.facelets"
xmlns:h="jakarta.faces.html"
xmlns:f="jakarta.faces.core"
xmlns:p="primefaces"
xmlns:o="http://omnifaces.org/ui"
template="/template/template.xhtml">

<ui:define name="title">
Ajax Framework <span class="subitem">WebSocket</span>
</ui:define>

<ui:define name="description">
WebSocket allows server side push events.
WebSocket allows server side push events using OmniFaces <code>o:socket</code>. Faces
<code>f:websocket</code> is not currently working
due to an open MyFaces defect: <a href="https://issues.apache.org/jira/projects/MYFACES/issues/MYFACES-4685"
rel="noopener" target="_blank">MYFACES-4685</a>
</ui:define>

<ui:define name="implementation">
<div class="card">
<h:form id="frmSocket">
<f:websocket scope="view" channel="pushChannel" onmessage="function(m){console.log(m);}">
<o:socket scope="view" channel="pushChannel" onmessage="function(m){console.log(m);}">
<f:ajax event="quarkusMessage" render="frmSocket:txt_count"/>
</f:websocket>
</o:socket>
<h1 class="text-center">
<h:outputText id="txt_count" value="#{webSocketView.count}"/>
</h1>
<p:commandButton value="Test" actionListener="#{webSocketView.sendMessage()}" process="@this"/>
</h:form>
</div>
</ui:define>
Expand Down
10 changes: 5 additions & 5 deletions src/main/resources/META-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@
<param-name>jakarta.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>jakarta.faces.ENABLE_WEBSOCKET_ENDPOINT</param-name>
<param-value>true</param-value>
</context-param>

<context-param>
<param-name>primefaces.PRIVATE_CAPTCHA_KEY</param-name>
Expand Down Expand Up @@ -122,6 +118,10 @@
</context-param>

<!-- Context parameters - OmniFaces -->
<context-param>
<param-name>org.omnifaces.SOCKET_ENDPOINT_ENABLED</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>org.omnifaces.COMBINED_RESOURCE_HANDLER_DISABLED</param-name>
<param-value>#{facesContext.application.projectStage eq 'Development'}</param-value>
Expand Down Expand Up @@ -226,4 +226,4 @@
<extension>svg#fontawesomeregular</extension>
<mime-type>image/svg+xml</mime-type>
</mime-mapping>
</web-app>
</web-app>

0 comments on commit 32b7baa

Please sign in to comment.