Skip to content
This repository has been archived by the owner on Nov 22, 2024. It is now read-only.

Enable Angular Universal with a Java backend #280

Closed
jeffwhelpley opened this issue Feb 24, 2016 · 72 comments
Closed

Enable Angular Universal with a Java backend #280

jeffwhelpley opened this issue Feb 24, 2016 · 72 comments

Comments

@jeffwhelpley
Copy link
Contributor

There are a couple potential ways to do this through Nashorn. Opening this issue to start the discussion and plan out how we can achieve this goal.

@cheungtitus
Copy link

@jeffwhelpley thanks for starting this and your referral to the doc at https://docs.google.com/document/d/1a3LDFEi6xV9vNNZxjoRwIw-Z3pgj_HAZXCJe8-gkAeA/edit#. This is awesome. Given Spring has already gone GA supporting Script Based Templating on Nashorn, just wondering if compatibility with Spring is part of the plan? @sdeleuze at the Spring team does intend to support Angular 2 server side rendering as mentioned on SPR-13508 so it'd be good to see the two teams are joined up and collaborating on this front. Maybe we can invite Sébastien to discuss on the next meet up?

@jeffwhelpley
Copy link
Contributor Author

@tbosch what do you think?

@sdeleuze
Copy link

Hey, indeed we would like to experiment with Spring support for Angular 2 server-side rendering as part of our Spring Framework 5 roadmap, and that would be awesome to be able to discuss that with you. You can find some information about our current support for isomorphic/universal applications here.

Script loader extension to load JS and resource files from the classpath is also a key topic I would be interested to discuss with you.

@manekinekko
Copy link
Contributor

@jeffwhelpley last week I started POC'ing Universal on Spring. I basically wanted to write a renderer for Spring.
My work is based on the work of @sdeleuze and other resources I found on the Internet. However, I was quickly blocked, because SystemJS (and even Webpack) are not supported by Nashorn.

Here is my repo for this POC: https://github.com/manekinekko/angular2-springboot-engine (BTW, I am not a Java guy so the code may seem messy or not well designed ^^)

Here is the issue I opened for SystemJS: systemjs/systemjs#1113

@sdeleuze I tried to contact one of your french colleagues at Pivotal, I met at DevFest Paris this year (Brian Clozel) without any success :)

@sdeleuze
Copy link

@manekinekko Thanks for your feedback and experiment. I will have a look shortly.
@bclozel reached me about you but we just had not the time to plan a meeting before our vacations.

@manekinekko
Copy link
Contributor

@sdeleuze I suspected Brian was on vacation ^^
You can ping me if you need more info, we are on the same time zone

@jeffbcross jeffbcross modified the milestone: Beta.0 Mar 18, 2016
@ryancampbell
Copy link

Hey there, I'm very interested in this as well (using the latest Grails with Angular Universal). Has anyone made further progress with this?

@jeffwhelpley
Copy link
Contributor Author

There are people working on this, but I have been out of it for a couple weeks getting ready for ng-conf. I will sync up with everyone next week and post a status here.

@ryancampbell
Copy link

Awesome thanks @jeffwhelpley
I would be interested in helping as well just don't want to duplicate work if it's already being tackled

@jeffwhelpley
Copy link
Contributor Author

@ryancampbell I will take you up on this! Let me get back in sync with Brad on this and will reach out to you within a week or so.

@sdeleuze
Copy link

sdeleuze commented May 9, 2016

I would be very interested by an update as well since we are close to begin active work on Spring Framework 5 where we would like to experiment and potentially provide support for Angular 2 running on Nashorn.

@cheungtitus
Copy link

@sdeleuze that'd be good. @jeffwhelpley sorry I started experimenting with Spring 4's Script Based Templating a couple of months ago but haven't gotten it to work yet, and then have been occupied with multiple family events. It'd be good to indeed have us all get synced up somehow.

@manekinekko
Copy link
Contributor

@sdeleuze check this my repo. We are having issues with the module loader, see more details here.

@sdeleuze
Copy link

sdeleuze commented May 9, 2016

@manekinekko Interesting, I guess we need to find the right polyfill to make it work ...

@jeffwhelpley
Copy link
Contributor Author

I am in the process of pulling together all interested parties in the Java-Universal integration. There are a couple internal folks at Google in addition to all of you in the community that have posted here (and many more I talked to at ng-conf last week). This effort has been moving along somewhat slowly up to now, but it is time to make it legit. I think the first step will be a quick meeting of people who may be interested in contributing to this effort. If you want to simply get updates on the status, then watch this issue. If you want to be more heavily involved, email me at jeff at gethuman dot com.

@TheLarkInn
Copy link
Member

Also interested in doing this in house, so +1

@bennylut
Copy link

j2v8 (https://github.com/eclipsesource/J2V8) just added support for linking (using jni) node.js into java applications - this may help reducing the amount of effort required for the port (instead of adapting nashorn)
didn't test it myself yet though..

@irbull
Copy link

irbull commented May 10, 2016

As the author of J2V8 I'll mention a few things. J2V8 is a set of Java bindings for V8. It exposes the V8 API to Java via JNI. Yesterday I finalized the support for node.js with J2V8. This means you start node itself (not just V8), execute scripts on node, and access the node message loop. You can also register Java methods to invoke when a JS Function is called, and you can call JS functions from Java. You can also get JS Objects and traverse them in Java. I haven't published binaries of the node integration yet, but I have the code to do that (I've built it on Linux and Mac, I'll try Windows next).

@TheLarkInn
Copy link
Member

@irbull could you paste a link to the repository? Excellent work!

@TheLarkInn
Copy link
Member

Nvm I see it above.

@ryancampbell
Copy link

Nice work!

@TheLarkInn
Copy link
Member

Only because I don't know Java, would you be willing to boilerplate this?

@bennylut
Copy link

I will try to make a small "hello angular-universal/j2v8" prototype and post here how it goes

@jeffwhelpley
Copy link
Contributor Author

@bennylut that would be awesome. I am trying to get everyone together interested in this topic, but it looks like a lot of the Googlers won't be available until after Google I/O which is next week. So, I will get something on the calendar for the following week and in the meantime try to document the integration spec.

@cheungtitus
Copy link

@bennylut @irbull The J2V8 is amazing. This looks really similar to handlebars.java in that we can run the JS inside Java. Also nice is the callback. Wow. I wrote a Angular2 based front end served by a Spring 4 back end with mock data a few months ago. Will try out J2V8 to see how it goes by having it rendered on the back end instead. BTW I happen to notice the POM artifacts doesn't have the Linux version listed. @sdeleuze does this mean we need to have a J2V8ViewResolver for Spring 5?

@salmar
Copy link

salmar commented May 11, 2016

@bennylut sounds great. We'll have the Spring I/O conference next week in Barcelona, would be nice to show some initial work on that direction.

@sdeleuze
Copy link

sdeleuze commented May 12, 2016

@bennylut Currently I guess the answer is yes, but ideally what I would prefer is using J2V8 through JSR 223 that we support natively. As you can see in ScriptTemplateView, we use ScriptEngine#eval(), Invocable#invokeFunction(), Invocable#invokeMethod(). @irbull Do you think that could be doable in theory?

In any case, I think both Nashorn (that is now quite good in latest Java 8 builds, and continue to evolve as part of upcoming Java 9 release to support ES6) and J2V8 are interesting to support. Usually with Nashorn, the trick is to load a small polyfil before the JS library to create (like this one for React) some JS global variables.

@bennylut
Copy link

@sdeleuze I don't see why one will have a problem writing JSR223 compatibility layer for J2V8.

Here are my two cents about Nashorn:

Nashorn is a great project, it is much faster than rhino - still not close to v8 though. But even if you don't mind the performance differences on your server side - using it can cause major headache since for each change in angular-universal or one of its dependencies one will have to test and make sure that both nashorn and its polyfills are compatible with the changes and make the proper fixes if not.

@mraible
Copy link

mraible commented May 3, 2017

@vikerman Let me know if I can help!

@swaechter
Copy link

@niconavent @petercn Sorry for not answering to your comments, there was just too much study/work things going on + I was ill and was unable to find time to polish my example.

But now I am able to provide a runnable version that is based on Angular 4, uses AOT and @angular/platform-server for rendering (Due the configuration I had to drop Angular CLI and use Webpack). You can find the project here: https://github.com/swaechter/spring-boot-angular-renderer

I plan to invest more time in the future, because I would like to use Java SSR in production and at work (Spring Boot backends, but other frameworks are of course also possible). Maybe I can also team up with @vikerman? The next steps are to fix J2V8 problems on Windows*, add preboot.js for capturing user events, add HTTP handling and live reloading. So I am really open for feedback (Create an issue or write a mail).

*For my daily work I use Linux and J2V8 works really well on Linux. It also works on Windows but I run into some strange DLL loading issues which I have to investigate in the next days

@vikerman
Copy link
Contributor

@swaechter - Will take a look at your project. I was going to run Node.JS as a separate process. Will check out the J2V8 bridge solution as well. Using a binary installation of J2V8 might be problematic in certain situations. I think we should be able abstract out the communication channel between Java and Node.JS and people can choose what works for them - In-process with J2V8 or out of process IPC. Does that sound like a good idea?

@dherges
Copy link

dherges commented May 10, 2017

@vikerman there is just one considerations:

if I go for IPC with node, I also could start a web server running on node, running Angular Universal on the node server and then go for a http proxy solution.

Either way, I think it's a good idea to offer both J2V8 (or Nashorn) and IPC

@petercn
Copy link

petercn commented Jun 12, 2017

@vikerman Is any initial solution available for this yet?

@petercn
Copy link

petercn commented Jul 25, 2017

Anyone have any updates on this issue?

@swaechter
Copy link

swaechter commented Jul 25, 2017

@petercn I published my repository as Maven library (See here https://mvnrepository.com/artifact/ch.swaechter/angularj-ssr). I plan to rename the project to angularj-universal and split it up into several modules including an example + documentation. I would also like to integrate angular-ssr to use preboot.js.

Keep in mind: My J2V8 solution based on the work of @bennylut (I owe you a beer) is only one possible implementation. If wished I can add other implementations like a solution that uses an IPC mechanism to communicate with a local NodeJS instance ( @vikerman Were you able to implement an IPC mechanism?)

@swaechter
Copy link

Sorry: I forgot to push my code to Github. Will do that now

@petercn
Copy link

petercn commented Jul 25, 2017

@swaechter Thanks for the excellent and prompt reply :-)

@swaechter
Copy link

swaechter commented Jul 25, 2017

@petercn Everything is ready now (Drop a mail if you encounter any problems):

I will invest more time in the next weeks (Finally (!) semester holidays). Although I got 10 and your followup question 7 likes, I don't really know what people/Java developer are expecting or looking/waiting for. So in case you liked these comments or are a Java developer:

  • What are you expecting from a Java server side rendering project?
  • What technologies are you using?
  • What is missing/roadblocking? What needs to be done?

What I am planing to do:

  • Integrate angular-ssr (https://github.com/clbond/angular-ssr) including preboot.js and drop renderModuleFactory()
  • Rename the project to angularj-universal, split up the modules and clean up the project
  • Write more examples and documentation
  • Discuss an implementation that communicates with a local NodeJS instance via IPC, so there is a second implementation besides J2V8

There was also a separate thread going on, but I guess we can discuss that now here (Link #721).

@srinivasvsk
Copy link

This is great progress, just few things which would help for spring developers

  1. We use quite a bit https://start.spring.io/ for generating starter projects, maybe adding this there would be great
  2. How to integrate spring security in to this.
  3. A gradle version of your example would be great

@petercn
Copy link

petercn commented Jul 26, 2017

@swaechter For the most part I'm simply looking for an angular SSR solution which can run on a JVM rather than needing a NodeJS instance. I'll definitely take your code for a spin and I totally agree with your roadmap, especially including preboot :-) Thanks again for your efforts and hopefully Angular / Angular CLI Team will take notice and begin to officially support Java back ends. The CLI team recently added this great documentation for the Node-based setup at least: https://github.com/angular/angular-cli/wiki/stories-universal-rendering

@swaechter
Copy link

swaechter commented Aug 14, 2017

@petercn In the last few weeks I played around with several projects. You can divide them into two groups:

1.) Projects that provide CommonJS module loading and [re]implement the Node.js API:

  • Avatar.js: Great, but dead
  • Nodyn: Great, but dead
  • Rhinodo: Great, but dead

2.) Projects that only provide CommonJS module loading directly or via shim/polyfill/third party (But no implementation of the Node.js API):

  • Nashorn: Great, out of the box, but no implementation of the Node.js API
  • RingoJS: Great, but no [full] implementation of the Node.js API
  • Rhino: Dead, superseded by Nashorn

As you see, all Node.js re-implementations are more or less dead (I guess this is due the development speed & impact of Node.js as a project/product) and all active JavaScript-only projects lack the Node.js API.

Personally I don't think a Java JVM/Non Node.js/V8 based solution is possible at the moment because the Node.js implementation of Angular Universal uses modules like util, stream, events, fs and timers. If it's possible to rewrite Angular Universal without using Node.js modules (Pure JavaScript), it would be possible to use Angular Universal in Nashorn or RhingoJS (That would be really awesome).

@petercn
Copy link

petercn commented Aug 14, 2017

@swaechter Thanks for that detailed feedback of your findings

@swaechter
Copy link

@petercn No problem. The integrated platform-server (https://github.com/angular/angular/tree/master/packages/platform-server) itself doesn't rely on Node.js (or I missed it), but it's third party libraries like parse5 (src/parse5_adapter.ts) and zone.js/dist/zone-node.js require Node.js. Maybe @gdi2290 and @MarkPieszak can shed some light on this if it's theoretical possible to use JS-only libraries or what their thoughts were (Would be interesting for me) :)

@ronaldjeremy
Copy link

What about https://github.com/apigee/trireme ?

@bfwg
Copy link

bfwg commented Dec 4, 2017

Hi, any updates on this thread?

@jobehi
Copy link

jobehi commented Jan 31, 2018

Hello there, any news ?

@swaechter
Copy link

swaechter commented Feb 25, 2018

@bfwg @jobehi I haven't forgotten you :)

There are several technical limitations at the moment that make it really hard to find a good and clean solution that is suitable for all environments. This was also the reason why I didn't want to release some half-working solutions.

I created a feature/discussion issue in the official Angular repo: angular/angular#22443

@swaechter
Copy link

@ronaldjeremy I also played around with Trireme but had some out of stack/memory problems due the JavaScript optimization (I can't remember exactly).

But the bigger problem is this one from the README.md:

Trireme supports two versions of Node.js:

0.10.32. This is the default, fully-supported version.
0.12.7. This version is still a work in progress.
Support for Node.js 4.0 depends on more complete ES6 code in Rhino. The Rhino community is making progress on this but it will be quite some time before we are ready to support 4.0.

You can find the release dates here: https://nodejs.org/en/download/releases/

@CaerusKaru CaerusKaru added this to the Backlog milestone Mar 7, 2018
@Toxicable
Copy link

Closed in favour of #1000

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests