Comments (3)
@imacrayon @devrav I pulled the cache
branch and added this snippet as a quick hack to get working SSE support in alpine-ajax, to see how well it would work:
Alpine.directive('sse', (el, { expression }, { cleanup }) => {
const es = new EventSource(expression, { withCredentials: true });
const handler = (event) => {
const response = {
ok: true,
url: event.target.url,
html: event.data,
};
let targets = findTargets(AjaxAttributes.get(el, 'targets', []))
if (targets.length) {
targets = addSyncTargets(targets)
}
render(response, el, targets, false, false)
};
es.addEventListener("html-fragment", handler);
cleanup(() => {
es.removeEventListener('html-fragment', handler);
es.close();
});
});
It works like a charm and I used it to enhance a few of my pages for a demo. It works great for my usecase for now, it increases the power-to-weight ratio of the library quite a bit since the server events pattern gets live events from a message bus, and it makes it possible write MPAs that progressively enhance to super-interactive collaborative applications where your changes show up immediately on other clients. I'm kind of tempted to implement some multiplayer games with it as well.
Feel free to use it for whatever usecase you have under the license of the original project or to make it more robust. It does suffer from the many downsides of the browser standard EventSource implementation, including not being able to pass headers and having a wonky reconnect logic, but for progressive enhancement usecases it's not a huge issue.
Alpine-Ajax is an amazing library with a great well-thought out API and great docs and I don't mind waiting for a first party implementation. The way targets are handled makes it incredibly easy to refactor an application between different ways to fetch fragments, and a non-SSE application I had basically only needed a one or two extra props to work and update on other peoples changes once the backend SSE endpoint was there, since all the ids and x-merge logic was already set up.
from alpine-ajax.
Hey, I am currently working on hx-boost style navigation, you can follow along in #65
I donβt have any immediate plans for SSE or websockets, but the refactoring Iβm doing in the PR above should help generalize some of the internals of Alpine AJAX. I think that could open up the possibility of building your own Alpine plugins that can use Alpine AJAX features.
from alpine-ajax.
Thanks for the quick response. Routing feature does add more value than the sse/websockets support at this time, glad to know that you are already working on it.
Thanks for putting in the effort, looking forward to seeing how the project progresses :)
from alpine-ajax.
Related Issues (20)
- Question on passing CSRF tags for Django HOT 1
- [BUG] x-merge=prepend,append,update does not seem to work HOT 3
- x-ref replace x-target HOT 15
- Bug when target is a tr
- TypeError in latest version HOT 2
- Able to set `enctype` to application/x-www-form-urlencoded HOT 1
- Add support for `autofocus`
- `MissingIdError` error thrown when using `x-target.replace` without an ID HOT 2
- Calling $ajax from code outside of Alpine.js HOT 5
- Use ajax:success to preload assets (js) HOT 3
- Could not find a declaration file for module '@imacrayon/alpine-ajax' HOT 3
- Any way to make incoming html replace the innerHTML of target instead of the whole target? HOT 2
- Uncaught (in promise) Missing Target: #[object HTMLButtonElement] was not found in the current document. HOT 5
- Using x-morph, new Alpine components don't initialize HOT 2
- Apply different merge strategy per target HOT 4
- Polling support? HOT 2
- Dynamically assigning a value for x-target not working, but works if assigning it statically. HOT 4
- Filterable content + infinite scroll: how to set x-merge attribute HOT 3
- v.0.6.0 broke the usual x-target behavior HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from alpine-ajax.