Repository navigation
fix: send push notifications as SSE events on the SSE transport #27258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
138a273
f620638
2166e93
c3a04cf
271e911
ff45543
2dede20
d8967e8
8850e91
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,6 +34,7 @@ | |
| import org.atmosphere.cpr.AtmosphereResource.TRANSPORT; | ||
| import org.atmosphere.cpr.AtmosphereResourceEvent; | ||
| import org.atmosphere.cpr.AtmosphereResourceImpl; | ||
| import org.atmosphere.cpr.FrameworkConfig; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import tools.jackson.core.JacksonException; | ||
|
|
@@ -663,6 +664,16 @@ private static void sendNotificationAndDisconnect( | |
| "sendNotificationAndDisconnect called for resource no longer in scope"); | ||
| return; | ||
| } | ||
| if (resource.transport() == TRANSPORT.SSE | ||
| && !resource.isSuspended()) { | ||
| // Atmosphere's SSE interceptor frames writes as events only on | ||
| // a suspended resource, or when this attribute is set, as it | ||
| // is for the handshake of a new connection. EventSource ignores | ||
| // anything that is not framed. | ||
| resource.getRequest().setAttribute( | ||
| FrameworkConfig.CALLBACK_JAVASCRIPT_PROTOCOL, | ||
| Boolean.TRUE); | ||
| } | ||
| resource.getResponse() | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This path never suspends the resource, and the write sets The establish path sets PushHandler.java:671 ·
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The response goes out as I also checked it in a real browser. The new |
||
| .setContentType(JsonConstants.JSON_CONTENT_TYPE); | ||
| resource.getResponse().getWriter().write(notificationJson); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /* | ||
| * Copyright 2000-2026 Vaadin Ltd. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
| * use this file except in compliance with the License. You may obtain a copy of | ||
| * the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| * License for the specific language governing permissions and limitations under | ||
| * the License. | ||
| */ | ||
| package com.vaadin.flow.uitest.ui.push; | ||
|
|
||
| import org.junit.Assert; | ||
| import org.junit.Test; | ||
| import org.junit.experimental.categories.Category; | ||
| import org.openqa.selenium.By; | ||
| import org.openqa.selenium.TimeoutException; | ||
|
|
||
| import com.vaadin.flow.testcategory.PushTests; | ||
| import com.vaadin.flow.testutil.ChromeBrowserTest; | ||
|
|
||
| @Category(PushTests.class) | ||
| public abstract class PushSessionExpiredIT extends ChromeBrowserTest { | ||
|
|
||
| @Test | ||
| public void sessionGone_pushReconnects_pageReloads() { | ||
| open(); | ||
| AbstractClientServerCounterIT.getIncrementButton(this).click(); | ||
| waitUntil(driver -> AbstractClientServerCounterIT | ||
| .getClientCounter(this) == 1); | ||
| // Survives push reconnects, but not a reload | ||
| executeScript("window.beforeSessionGone = true"); | ||
|
|
||
| // As after a server restart: the server closes the push connection, | ||
| // and the session is gone when the client opens a new one | ||
| findElement(By.id(BasicPushView.CLOSE_PUSH_LATER_ID)).click(); | ||
| getDriver().manage().deleteCookieNamed("JSESSIONID"); | ||
|
|
||
| // The new push connection is answered with the session expired | ||
| // notification, which reloads the page by default. If the client | ||
| // misses the notification, it keeps opening new push connections and | ||
| // the page is not reloaded. | ||
| try { | ||
| waitUntil( | ||
| driver -> Boolean.TRUE.equals(executeScript( | ||
| "return window.beforeSessionGone === undefined")), | ||
| 20); | ||
| } catch (TimeoutException e) { | ||
| Assert.fail("The page was not reloaded after the session was gone"); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| * Copyright 2000-2026 Vaadin Ltd. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
| * use this file except in compliance with the License. You may obtain a copy of | ||
| * the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| * License for the specific language governing permissions and limitations under | ||
| * the License. | ||
| */ | ||
| package com.vaadin.flow.uitest.ui.push; | ||
|
|
||
| import org.junit.experimental.categories.Category; | ||
|
|
||
| import com.vaadin.flow.component.Component; | ||
| import com.vaadin.flow.testcategory.IgnoreOSGi; | ||
| import com.vaadin.flow.testcategory.PushTests; | ||
|
|
||
| @Category({ PushTests.class, IgnoreOSGi.class }) | ||
| public class PushSessionExpiredLongPollingIT extends PushSessionExpiredIT { | ||
|
|
||
| @Override | ||
| protected Class<? extends Component> getViewClass() { | ||
| return BasicPushLongPollingView.class; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| * Copyright 2000-2026 Vaadin Ltd. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
| * use this file except in compliance with the License. You may obtain a copy of | ||
| * the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| * License for the specific language governing permissions and limitations under | ||
| * the License. | ||
| */ | ||
| package com.vaadin.flow.uitest.ui.push; | ||
|
|
||
| import org.junit.experimental.categories.Category; | ||
|
|
||
| import com.vaadin.flow.component.Component; | ||
| import com.vaadin.flow.testcategory.IgnoreOSGi; | ||
| import com.vaadin.flow.testcategory.PushTests; | ||
|
|
||
| @Category({ PushTests.class, IgnoreOSGi.class }) | ||
| public class PushSessionExpiredSseIT extends PushSessionExpiredIT { | ||
|
|
||
| @Override | ||
| protected Class<? extends Component> getViewClass() { | ||
| return BasicPushSseView.class; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| * Copyright 2000-2026 Vaadin Ltd. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
| * use this file except in compliance with the License. You may obtain a copy of | ||
| * the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| * License for the specific language governing permissions and limitations under | ||
| * the License. | ||
| */ | ||
| package com.vaadin.flow.uitest.ui.push; | ||
|
|
||
| import org.junit.experimental.categories.Category; | ||
|
|
||
| import com.vaadin.flow.component.Component; | ||
| import com.vaadin.flow.testcategory.IgnoreOSGi; | ||
| import com.vaadin.flow.testcategory.PushTests; | ||
|
|
||
| @Category({ PushTests.class, IgnoreOSGi.class }) | ||
| public class PushSessionExpiredWebsocketIT extends PushSessionExpiredIT { | ||
|
|
||
| @Override | ||
| protected Class<? extends Component> getViewClass() { | ||
| return BasicPushWebsocketView.class; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does this location need to take the transport into account if no other location needs to?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Artur- This is the only place that writes straight to the response of a push resource. All other push messages go through the
Broadcaster(AtmospherePushConnection.sendMessage) to a suspended resource, and Atmosphere's interceptors add each transport's framing there, includingdata:for SSE.Here the resource is usually not suspended yet (the connection is rejected in
onConnector the first receive). On that path:TrackMessageSizeInterceptoradds that whether or not the resource is suspended. So the plain JSON write works for them.SSEAtmosphereInterceptoronly writes padding on the first write to an unsuspended resource. It skips thedata:prefix and the closing blank line, soEventSourcedrops the message. That's why only SSE needs its own branch, and only while the resource is unsuspended.The other option would be to suspend the resource first and broadcast the notification, so Atmosphere frames it for every transport. That would change the disconnect flow for all transports to fix one, so I kept the change local. I can switch to that approach if you prefer it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would sound better not to do low level writes but how large is the blast radius?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Artur- Good point. I replaced the low-level write, so the blast radius stays the same as before: only SSE resources that are not suspended yet.
Atmosphere's SSE interceptor already frames writes on an unsuspended resource when the request has the
FrameworkConfig.CALLBACK_JAVASCRIPT_PROTOCOLattribute. Atmosphere'sJavaScriptProtocolsets it for the handshake of a new connection. So the notification now goes through the normal Atmosphere writer, andPushHandleronly sets that attribute first. Atmosphere adds the padding, thedata:framing and the message size prefix itself.I checked the wire output on embedded Jetty with real Atmosphere, using a push request without a session:
45|{...sessionExpired...}(unframed), afterdata:45|{...}followed by a blank line.0):data:45|{...}both before and after, because Atmosphere already set the attribute there.So the bug only hit reconnects, which is the idle-tab case. WebSocket, long polling and already open SSE connections are untouched. A test now checks that WebSocket requests don't get the attribute.