- Coherence 6 0 – Turn Websites Into Apps Free
- Coherence 6 0 – Turn Websites Into Apps Using
- Coherence 6 0 – Turn Websites Into Apps Pdf
- Coherence 6 0 – Turn Websites Into Apps Without
Before Proceeding:
Consult 'Supported Web Containers' to see if you must perform any application server-specific installation steps.When deploying Coherence*Web on WebLogic Server you now have two options:
By Ethan A New Approach for Blending Web Engine into Native Apps What if I told you the 7 lines of JSON above, colored in orange is all you need to turn a website into a mobile app? No need to rewrite your website using some framework API just to make it behave like a mobile app. Just bring your existing website as is, and blend it into a native app with a simple URL reference. Coherence X 3.0.2 Description. Coherence X – The perfect middle-ground between websites and apps on your Mac. Coherence X is a powerful tool that allows you to turn any websites into a chromium-based, native application on your Mac. Features An isolated app in seconds. Coherence X is a powerful tool that allows you to turn any websites into a chromium-based, native application on your Mac. Coherence X builds upon the fundamentals of the world's previous most powerful SSB tool - Coherence Pro 2 - and rebuilds them from the ground up. Here are just some of the major new changes. OS X 10.10 or later 64-bit DOWNLOAD.
- Use the WebInstaller approach described in this chapter.
- Use the SPI-based installation for WebLogic Server 11gR1 or later. See Chapter 2, 'Using Coherence*Web with WebLogic Server.'
When deploying Coherence*Web on GlassFish Server, use the SPI-based installation described in Chapter 3, 'Using Coherence*Web with GlassFish Server'.
- The perfect middle-ground between websites and apps on your Mac. Coherence X is a powerful tool that allows you to turn any websites into a chromium-based, native application on your Mac. Coherence X builds upon the fundamentals of the world’s previous most powerful SSB tool – Coherence Pro 2 – and rebuilds them from the ground up.
- Coherence is an app based on Google Chrome that allows you to turn your favorite websites into apps. Simply name your application, type in the URL, give it an icon, and it instantly makes an application that can be put on your dock, used in full screen, put on your desktop, or anything that any other Mac application can do.
This chapter provides instructions on how to use the Coherence*Web WebInstaller to install Coherence*Web for Java EE applications on a variety of different application servers.
This chapter contains the following sections:
4.1 Installing Coherence*Web Using the WebInstaller
Coherence*Web can be enabled for Java EE applications on several different Web containers. To do this, you must run the ready-to-deploy application through the automated Coherence*Web WebInstaller before deploying it. The automated installer prepares the application for deployment. It performs the installation process in two discrete steps: an inspection step and an installation step. For more information about what the installer does during these steps, see 'How the Coherence*Web WebInstaller Instruments a Java EE Application'.
The installer can be run either from the Java command line or from Ant tasks. The following sections describe the Java command-line method. For Ant task-based installation, see 'Coherence*Web WebInstaller Ant Task'.
4.1.1 Application Server-Specific Installation Instructions
All of the Web containers listed in 'Supported Web Containers' that can be installed with the WebInstaller share the same general installation instructions. These instructions are described in 'General Instructions for Installing Coherence*Web Session Management Module'.
A few of the Web containers, such as Caucho Resin, and WebLogic 10.x, require extra, container-specific steps that you must complete before starting the general installation procedure. The following sections describe application server-specific installation steps:
4.1.1.1 Installing on Oracle WebLogic Server 10.x
Complete the following steps to install the Coherence*Web Session Management Module into Oracle WebLogic Server release 10 to 10.2:
- From within the Coherence library directory, extract the
coherence-web.jar
file from thewebInstaller.jar
file:This command extracts thecoherence-web.jar
file into a subdirectory namedweb-install
. Use the following commands to move thecoherence-web.jar
file up one level into the library directory:On Windows:On UNIX: - For each WebLogic Server 10.x installation that will be running in the server cluster, update the libraries using the following command:For example, on Windows:
- Follow the instructions described in 'General Instructions for Installing Coherence*Web Session Management Module' to complete the installation. Use the value
WebLogic/10.x
for the server type.
4.1.1.2Installing on Caucho Resin 3.1.x
Complete the following steps to install the Coherence*Web Session Management Module into a Caucho Resin 3.1.x server:
- From within the Coherence library directory, extract the
coherence-web.jar
file from thewebInstaller.jar
file:This command extracts thecoherence-web.jar
file into a subdirectory namedweb-install
. Use the following commands to move thecoherence-web.jar
file up one level into the library directory:On Windows:On UNIX: - For each Caucho Resin installation that will be running in the server cluster, update the libraries using the following command:For example, on Windows:
- Follow the instructions described in 'General Instructions for Installing Coherence*Web Session Management Module' to complete the installation. Use the value
Resin/3.1.x
for the server type.
4.1.2 General Instructions for Installing Coherence*Web Session Management Module
Complete the following steps to install Coherence*Web for a Java EE application on any of the Web containers listed under 'Supported Web Containers'.
If you are installing Coherence*Web for a Java EE application on an Apache Tomcat Server, see also 'Enabling Sticky Sessions for Apache Tomcat Servers' for additional instructions.
If you are installing Coherence*Web for a Java EE application on IBM WebSphere Server, see also 'Decoding URL Session IDs for IBM WebSphere 7.x Servers' for additional instructions.
To install Coherence*Web for the Java EE application you are deploying:
- Ensure that the application directory and the EAR file or WAR file are not being used or accessed by another process.
- Change the current directory to the Coherence library directory (
%COHERENCE_HOME%
lib
on Windows and$COHERENCE_HOME
/lib
on UNIX). - Ensure that the paths are configured so that Java commands will run.
- Complete the application inspection step by running the following command. Specify the full path to your application and the name of your server found in Table 1-1 (replacing the
<app-path>
and<server-type>
with them in the following command line):The system will create (or update, if it already exists) thecoherence-web.xml
configuration descriptor file for your Java EE application in the directory where the application is located. This configuration descriptor file contains the default Coherence*Web settings for your application as recommended by the installer. - If necessary, review and modify the Coherence*Web settings based on your requirements.You can modify the Coherence*Web settings by editing the
coherence-web.xml
descriptor file. Appendix A, 'Coherence*Web Context Parameters,' describes the Coherence*Web settings that can be modified. Use theparam-name
andparam-value
subelements of thecontext-param
parameter to enable the features you want. Table 4-1 describes some examples of different settings.Table 4-1 Example Context Parameter Settings for Coherence*WebParameter Name Description coherence-servletcontext-clustered
trueClusters allServletContext
(global) attributes so that servers in a cluster share the same values for those attributes, and also receive the events specified by the Servlet Specification when those attributes change.coherence-enable-sessioncontext
trueAllows an application to enumerate all of the sessions that exist within the application, or to obtain any one of those sessions to examine or manipulate.coherence-session-id-length
32
Enables you to increase the length of theHttpSession
ID, which is generated using aSecureRandom
algorithm; the length can be any value, although in practice it should be small enough to fit into a cookie or a URL (depending on how session IDs are maintained.) Increasing the length can decrease the chance of a session being purposely hijacked.coherence-session-urlencode-enabled
trueBy default, theHttpSession
ID is managed in a cookie. If the application supports URL encoding, this option enables it. - Complete the Coherence*Web application installation step by running the following command, replacing
<app-path>
with the full path to your application:The installer requires a validcoherence-web.xml
configuration descriptor file to reside in the same directory as the application. The command creates asession-cache-config.xml
file in theWEB-INFclasses
directory of the application archive file. This file contains the session and cache configuration information. - Deploy the updated application and verify that everything functions as expected, using the lightweight load balancer provided with the Coherence distribution. Remember that the lightweight load balancer is not a production-ready utility, in contrast to the load balancer provided by WebLogic Server.
The application can be deployed and run in any of the deployment topologies supported by Coherence: in-process, out-of-process, or out-of-process with Coherence*Extend. See the following sections for information on deploying and running your applications under these topologies. For more information on the topologies themselves, see 'Deployment Topologies'.
4.1.2.1 Deploying and Running Applications In Process
Coherence*Web can be run in-process with the application server. This is where session data is stored with the application server. See 'In-Process Topology' for more information on this topology.
The application will use the cache and session configuration information in the
session-cache-config.xml
file. The Coherence*Web WebInstaller creates this file for you as part of the -install
step and stores it in the WEB-INFclasses
directory of the instrumented application.For the application server:
- Start the application server in storage-enabled mode. Add the system property
tangosol.coherence.session.localstorage=true
to the Java options of your application server startup script. - Deploy the
coherence.jar
andcoherence-web.jar
files as shared libraries. - Deploy and run your application.
4.1.2.2 Deploying and Running Applications Out-of-Process
In the out-of-process deployment topology, a stand-alone cache server stores the session data and the application server is configured as a cache client. See 'Out-of-Process Topology' for more information on this topology.
The cache server and the application server must use the same cache and session configuration. This configuration is generated in the
session-cache-config.xml
file by the Coherence*Web WebInstaller. The WebInstaller generates the file in the WEB-INFclasses
directory of the instrumented application.For the cache server:
- Add the
tangosol.coherence.cacheconfig
system property to the cache server startup script to locate the file configuration file. You must also include the system propertytangosol.coherence.session.localstorage=true
to enable storage for the cache server. - Add the application's WAR file, and the
coherence.jar
andcoherence-web.jar
files to the classpath in the cache server startup script.Following is a sample startup script:
For the application server (cache client):
- Deploy the
coherence.jar
andcoherence-web.jar
files as shared libraries. - The
session-cache-config.xml
file should already be present in theWEB-INFclasses
directory of the instrumented application.By default, the file should specify that local storage is disabled (if you are not sure, you can either inspect the file to confirm that thelocal-storage
element is set tofalse
or add the system propertytangosol.coherence.session.localstorage=false
to the startup script). - Deploy the application to the server.
4.1.2.3 Migrating to Out-of-Process Topology
If you have been running and testing your application with Coherence*Web in-process, you can easily migrate to the out-of-process topology. Simply set up your cache server and application server as described in 'Deploying and Running Applications Out-of-Process'.
4.1.2.4 Deploying and Running Applications Out-of-Process with Coherence*Extend
The out-of-process with Coherence*Extend topology is similar to the out-of-process topology except that the communication between the application server tier and the cache server tier is over Coherence*Extend (TCP/IP). Coherence*Extend consists of two components: an extend client (or proxy) running outside the cluster and an extend proxy service running in the cluster hosted by one or more cache servers. See 'Out-of-Process with Coherence*Extend Topology' for more information on this topology.
In these deployments, there are three types of participants:
- Cache servers (storage servers), which are used to store the actual session data in memory.
- Web (application) servers, which are the Extend clients in this topology. They are not members of the cluster; instead, they connect to a proxy node in the cluster that will issue requests to the cluster on their behalf.
- Proxy servers, which are storage-disabled members (nodes) of the cluster that accept and manage TCP/IP connections from Extend clients. Requests that arrive from clients will be sent into the cluster, and responses will be sent back through the TCP/IP connections.
For the cache server:
Follow the instructions for configuring the cache server in 'Deploying and Running Applications Out-of-Process'. Also, edit the cache server's copy of the
session-cache-config.xml
file to add the system properties tangosol.coherence.session.proxy=false
and tangosol.coherence.session.localstorage=true
.See 'Configure the Cache for Proxy and Storage JVMs' for more information and an example of a
session-cache-config.xml
file with these context parameters.For the Web tier (application) server:
Follow the instructions for configuring the application server in 'Deploying and Running Applications Out-of-Process'. Also, complete these steps:
- Sketch 41 – vector drawing application. Ensure that Coherence*Web is configured to use the Optimistic Locking mode. Optimistic locking is the default locking mechanism for Coherence*Web (see'Optimistic Locking').
- Edit the application server's copy of the
session-cache-config.xml
file to add the proxy JVM host names, IP addresses and ports. To do this, add a<remote-addresses>
section to the file. In most cases, you should include the host name and IP address, and port of all proxy JVMs for load balancing and failover.
See 'Configure the Cache for Web Tier JVMs' for more information and an example of a
session-cache-config.xml
file with a <remote-addresses>
section.For the proxy server:
With a few changes, the proxy server can use the same cache and session configuration as the application server and the cache server. Edit the
session-cache-config.xml
file to add these system properties:tangosol.coherence.session.localstorage=false
to disable local storage.tangosol.coherence.session.proxy=true
to indicate that a proxy service is being used.tangosol.coherence.session.proxy.localhost
to indicate the host name or IP address of the NIC to which the proxy will bind.tangosol.coherence.session.proxy.localport
to indicate a unique port number to which the proxy will bind.
See 'Configure the Cache for Proxy and Storage JVMs' for more information and an example of a
session-cache-config.xml
file with these context parameters.4.1.3 Enabling Sticky Sessions for Apache Tomcat Servers
If you want to employ sticky sessions for the Apache Tomcat Server, you must configure the
jvmRoute
attribute in the server's server.xml
file. You can find more information on this attribute at this URL:4.1.4 Decoding URL Session IDs for IBM WebSphere 7.x Servers
If set to
true
, the coherence-session-urldecode-bycontainer
context parameter allows the container to decode the URL. This context parameter must be set to false
if you are installing Coherence*Web for a Java EE application on release 7.x of the IBM WebSphere application server. Instead of the WebSphere application server, Coherence*Web will handle the decoding of session IDs.The Coherence*Web WebInstaller, when run for the WebSphere 7.x application server type, will automatically set this parameter to
false
unless you explicitly set it to true
.4.2 Coherence*Web WebInstaller Ant Task
The Coherence*Web WebInstaller Ant task enables you to run the installer from within your existing Ant build files.
This section contains the following information:
4.2.1 Using the Coherence*Web WebInstaller Ant Task
To use the Coherence*Web WebInstaller Ant task, add the task import statement illustrated in Example 4-1 to your Ant build file. In this example,
${
coherence.home
}
refers to the root directory of your Coherence installation.Example 4-1 Task Import Statement for Coherence*Web WebInstaller
The following procedure describes the basic process of installing Coherence*Web into a Java EE application from an Ant build:
- Build your Java EE application as you ordinarily would.
- Run the Coherence*Web Ant task with the
operations
attribute set toinspect
. - Make any necessary changes to the generated Coherence*Web XML descriptor file.
- Run the Coherence*Web Ant task with the
operations
attribute set toinstall
.
Performing Iterative Development
If you are performing iterative development on your application, such as modifying JavaServer Pages (JSPs), Servlets, static resources, and so on, use the following installation process:
- Run the Coherence*Web Ant task with the
operations
attribute set touninstall
, thefailonerror
attribute set tofalse
, and thedescriptor
attribute set to the location of the previously generated Coherence*Web XML descriptor file (from Step 2 of 'Using the Coherence*Web WebInstaller Ant Task'). - Build your Java EE application as you ordinarily would.
- Run the Coherence*Web Ant task with the
operations
attribute set toinspect
, and theinstall
anddescriptor
attributes set to the location of the previously generated Coherence*Web XML descriptor file (from Step 2 of 'Using the Coherence*Web WebInstaller Ant Task').
Changing the Coherence*Web Configuration Settings of a Java EE Application
If you must change the Coherence*Web configuration settings of a Java EE application that is using Coherence*Web, follow these steps:
- Run the Coherence*Web Ant task with the
operations
attribute set touninstall
and thedescriptor
attribute set to the location of the Coherence*Web XML descriptor file for the Java EE application. - Change the necessary configuration parameters in the Coherence*Web XML descriptor file.
- Run the Coherence*Web Ant task with the
operations
attribute set toinstall
and thedescriptor
attribute set to the location of the modified Coherence*Web XML descriptor file (from Step 2 of 'Using the Coherence*Web WebInstaller Ant Task').
4.2.2 Configuring the WebInstaller Ant Task
Table 4-2 describes the attributes that can be used with the Coherence*Web WebInstaller Ant task.
Table 4-2 Coherence*Web WebInstaller Ant Task Attributes
Attribute | Description | Required? |
---|---|---|
app | Path to the target Java EE application. This can be a path to a WAR file, an EAR file, an expanded WAR directory, or an expanded EAR directory. | Yes, if the operations attribute is set to any value other than version . |
backup | Path to a directory that holds a backup of the original target Java EE application. This attribute defaults to the directory that contains the Java EE application. | No |
descriptor | Path to the Coherence*Web XML descriptor file. This attribute defaults to the coherence-web.xml file in the directory that contains the target Java EE application. | No |
failonerror | Stops the Ant build if the Coherence*Web installer exits with a status other than 0. The default is true . | No |
nowarn | Suppresses warning messages. This attribute can be either true or false . The default is false . | No |
operations | A comma- or space-separated list of operations to perform; each operation must be one of inspect , install , uninstall , or version . | Yes |
server | The alias of the target Java EE application server. | No |
touch | Touches JSPs and TLDs that are modified by the Coherence*Web installer. This attribute can be either true , false , or M/d/y h:mm a ' The default is false . | No |
verbose | Displays verbose output. This attribute can be either true or false . The default is false . | No |
4.2.3 WebInstaller Ant Task Examples
The following list provides sample commands for the WebInstaller Ant task.
- Inspect the
myWebApp.war
Web application and generate a Coherence*Web XML descriptor file calledmy-coherence-web.xml
in the current working directory: - Install Coherence*Web into the
myWebApp.war
Web application using the Coherence*Web XML descriptor file calledmy-coherence-web.xml
found in the current working directory: - Uninstall Coherence*Web from the
myWebApp.war
Web application: - Install Coherence*Web into the
myWebApp.war
Web application located in the/dev/myWebApp/build
directory using the Coherence*Web XML descriptor file calledmy-coherence-web.xml
found in the/dev/myWebApp/src
directory, and place a backup of the original Web application in the/dev/myWebApp/work
directory: - Install Coherence*Web into the
myWebApp.war
Web application located in the/dev/myWebApp/build
directory using the Coherence*Web XML descriptor file calledcoherence-web.xml
found in the/dev/myWebApp/build
directory. If the Web application has not already been inspected (that is,/dev/myWebApp/build/coherence-web.xml
does not exists); inspect the Web application before installing Coherence*Web: - Reinstall Coherence*Web into the
myWebApp.war
Web application located in the/dev/myWebApp/build
directory, using the Coherence*Web XML descriptor file calledmy-coherence-web.xml
found in the/dev/myWebApp/src
directory:
4.3 Testing HTTP Session Management
Coherence comes with a lightweight software load balancer; it is intended only for testing purposes. The load balancer is very easy to use and is very useful when testing functionality such as session management. Follow these steps to test HTTP session management with the lightweight load balancer:
- Start multiple application server processes on one or more server machines, each running your application on a unique IP address and port combination.
- Open a command (or shell) window.
- Change the current directory to the Coherence library directory (
%COHERENCE_HOME%
lib
on Windows and$COHERENCE_HOME
/lib
on UNIX). - Ensure that paths are configured so that Java commands will run.
- Start the software load balancer with the following command lines (each of these command lines makes the application available on the default HTTP port
80
).For example, to test load balancing locally on one machine with two application server instances on ports7001
and7002
:To run the load balancer locally on a machine namedserver1
that load balances to port7001
onserver1
,server2
, andserver3
:Assuming that you use the preceding command line, an application that previously was accessed with the URLhttp://server1:7001/my.jsp
would now be accessed with the URLhttp://server1:80/my.jsp
or justhttp://server1/my.jsp
.Note:Ensure that your application uses only relative redirections or the address of the load balancer.Table 4-3 describes the command-line options for the load balancer:Table 4-3 Load Balancer Command-Line OptionsOption Description backlog
Sets the TCP/ IP accept backlog option to the specified value, for example:-backlog=64
random
Specifies the use of a random load-balancing algorithm (default).roundrobin
Specifies the use of a round-robin load-balancing algorithmthreads
Uses the specified number of request or response thread pairs (so the total number of additional daemon threads will be two times the specified value), for example:-threads=64
.
4.4 How the Coherence*Web WebInstaller Instruments a Java EE Application
During the inspection step, the Coherence*Web WebInstaller performs the following tasks:
- Generates a template
coherence-web.xml
configuration file that contains basic information about the application and target Web container along with a set of default Coherence*Web configuration context parameters appropriate for the target Web container. See Appendix A, 'Coherence*Web Context Parameters' for descriptions of all possible parameters.The WebInstaller sets the servlet container to start in storage-disabled mode (that is, it setstangosol.coherence.session.localstorage
tofalse
).If an existingcoherence-web.xml
configuration file exists (for example, from a previous run of the Coherence*Web WebInstaller), the context parameters in the existing file are merged with those in the generated template. - Enumerates the JSP from each Web application in the target Java EE application and adds information about each JSP to the
coherence-web.xml
configuration file. - Enumerates the TLDs from each Web application in the target Java EE application and adds information about each TLD to the
coherence-web.xml
configuration file.
During the installation step, the Coherence*Web WebInstaller performs the following tasks:
- Creates a backup of the original Java EE application so that it can be restored during the uninstallation step.
- Adds the Coherence*Web configuration context parameters generated in Step 1 of the inspection step to the
web.xml
descriptor file of each Web application contained in the target Java EE application. - Unregisters any application-specific
ServletContextListener
,ServletContextAttributeListener
,ServletRequestListener
,ServletRequestAttributeListener
,HttpSessionListener
, andHttpSessionAttributeListener
classes (including those registered by TLDs) from each Web application. - Registers a Coherence*Web
ServletContextListener
class in eachweb.xml
descriptor file. At run time, the Coherence*WebServletContextListener
class propagates eachServletContextEvent
event to each application-specificServletContextListener
listener. - Registers a Coherence*Web
ServletContextAttributeListener
listener in eachweb.xml
descriptor file. At run time, the Coherence*WebServletContextAttributeListener
propagates eachServletContextAttributeEvent
event to each application-specificServletContextAttributeListener
listener. - Wraps each application-specific
Servlet
declared in eachweb.xml
descriptor file with a Coherence*WebSessionServlet
. At run time, each Coherence*WebSessionServlet
delegates to the wrappedServlet
. - Adds the following directive to each JSP enumerated in Step 2 of the inspection step:
During the uninstallation step, the Coherence*Web WebInstaller replaces the instrumented Java EE application with the backup of the original version created in Step (1) of the installation process.
4.5 Installing Coherence*Web into Applications Using Java EE Security
Note:
This section does not apply to the native WebLogic Server SPI implementation of Coherence*Web. It applies only if you are using the WebInstaller to install Coherence*Web into an application that uses Java EE security. For instructions on using the SPI implementation, see Chapter 2, 'Using Coherence*Web with WebLogic Server.'To install Coherence*Web into an application that uses Java EE security, follow these additional steps during installation:
- Enable Coherence*Web session cookies.See the
coherence-session-cookies-enabled
configuration element in Table A-1 for additional details. - Change the Coherence*Web session cookie name to a name that is different from the one used by the target Web container.By default, most containers use
JSESSIONID
for the session cookie name, so a good choice for the Coherence*Web session cookie name isCSESSIONID
. See thecoherence-session-cookie-name
configuration element in Table A-1 for additional details. - Enable session replication for the target Web container.If session replication is not enabled, or the container does not support a form of session replication, then you will be forced to re-authenticate to the Web application during failover. See your Web container's documentation for instructions on enabling session replication.
This configuration causes two sessions to be associated with a given authenticated user:
- A Coherence*Web session that contains all session data created by the Web application
- A session created by the Web container during authentication that stores only information necessary to identify the user
by Ethan
A New Approach for Blending Web Engine into Native Apps
What if I told you the 7 lines of JSON above, colored in orange is all you need to turn a website into a mobile app? No need to rewrite your website using some framework API just to make it behave like a mobile app. Just bring your existing website as is, and blend it into a native app with a simple URL reference.
And what if, just by tweaking the JSON markup a bit, you can access all the native APIs, native UI components, as well as native view transitions out of the box?
Here’s what a minimal example looks like in action:
Notice how I’ve embedded a github.com web page but the rest of the layout is all native UI components, such as the navigation header and the bottom tab bar. And the transition is automatically native without you having to rewrite the website using any APIs.
Before I explain how, you may ask: “That’s cool, but can you do anything meaningful other than just displaying the web page in a native app frame?”
Great question, because that’s the main topic of this post. All you need to do is create a seamless 2-way communication channel between the web view and the app, so the parent app can trigger any JavaScript functions inside the web view and the web view can reach outside to call native APIs.
Here’s one such example:
Note that this view contains:
- Native navigation header, complete with built-in transition functionality
- A Web view, which embeds a QR code generator web app
- A native chat input component at the bottom
All this can be described by just tweaking some of the JSON markup attributes we saw above.
Finally, note that the QR code changes as you enter something from the chat input. The chat input triggers a JavaScript function inside the QR code web app that re-generates the image.
No app development framework has tried to fundamentally solve this problem of “seamless integration of web view into native apps” because they’re all focused on picking either 100% native or 100% HTML5 side.
Whenever you hear someone talk about the future of mobile apps, you would probably hear them talk about “Will it be the HTML5 approach that wins out? Or will it be native?”
None of them see
native
and html
as something that could co-exist and furthermore, create synergy and achieve things that are not easily possible otherwise.In this article I’m going to explain:
- Why blending web engine and native components is often a good idea.
- Why a seamless integration of HTML and Native is not easy, and how I implemented one.
- Most importantly, how YOU can use it to build your own app instantly.
Why would you use HTML in a native app?
Before we go further, let’s first discuss whether this is even a good idea, and when you may want to take this approach. Here are some potential use cases:
1. Use Web Native Features
Some parts of your app may be better implemented using the web engine. For example, Websocket is a web-native feature that’s designed for the web environment. In this case it makes sense to use the built-in web engine (WKWebView for iOS and WebView for Android) instead of installing a 3rd party library that essentially “emulates” Websocket.
No need to install additional code just to do something that you can do for free, which brings us to the next point.
2. Avoid Large Binary Size
You may want to quickly incorporate features that will otherwise require a huge 3rd party library.
For example, to incorporate a QR code image generator natively, you will need to install some 3rd party library which will increase the binary size. But if you use the web view engine and a JavaScript library through a simple
<script s
rc>, you get all that for free, and you don’t need to install any 3rd party native libraries.3. No Reliable Mobile Library Exists
For some cutting edge technologies, there is no reliable and stable mobile implementation yet.
Fortunately most of these technologies have web implementations, so the most efficient way to integrate them is to use their JavaScript library.
4. Build part-native, part-web-based apps
Many new developers looking to port their website into a mobile app get discouraged or overwhelmed when they find out some of their existing website features are too complex to quickly rewrite from scratch for each mobile platform.
For example, you may have a single web page that’s too complex to immediately convert to a mobile app, but the rest of your website may be easily converted.
In this case, it would be nice if there was a way to build most of the app natively, but for that particular complex web page, somehow seamlessly integrate it into the app as HTML.
How does it work?
A. Jasonette
Jasonette is an open source, markup-based approach to building cross-platform native apps.
It’s like a web browser, but instead of interpreting HTML markup into web pages, it interprets JSON markup into native apps on iOS and Android.
Just like how all web browsers have exactly the same code but can deliver you all kinds of different web apps by interpreting various HTML markup on demand, all Jasonette apps have exactly the same binary, and it interprets various JSON markup on demand to create your app. The developers never need to touch the code. Instead, you build apps by writing a markup that translates to native app in real-time.
You can learn more about Jasonette here.
While Jasonette at its core is all about building native apps, this particular article is about integrating HTML into the core native engine, so let’s talk about that.
B. Jasonette Web Container
Native apps are great but sometimes we need to make use of web features.
But integrating web views into a native app is a tricky business. A seamless integration requires:
- Web view should be integrated as a part of native layout: The web view should blend into the app as a part of the native layout and is treated just like any other native UI components. Otherwise it will feel clunky, and it will feel exactly like what it is — a website.
- Parent app can control child web container: The parent app should be able to freely control the child web view.
- Child web container can trigger native events on the parent app: The child app should be able to trigger the parent app’s events to run native APIs.
These are a lot of work, so I first worked on only the first piece of the puzzle — simply embedding a web container into native layout — and released it as version 1:
JSON Web Container
HTML inside JSON Turns into Native App Componentsjasonette.com
HTML inside JSON Turns into Native App Componentsjasonette.com
This was already pretty useful, but it still had the limitation of being non-interactive.
The parent app couldn’t control the child web container, and the child couldn’t notify the parent of any event, keeping the web container completely isolated from the outside world.
C. Jasonette Web Container 2.0: Make it Interactive
After releasing version 1, I experimented with the second piece of the puzzle — adding interactivity to the web container.
The next section explains the solutions that were added to make the previously-static web containers interactive, making them significantly more powerful.
Implementation: Interactive Web Container
1. Load by URL
Problem
Previously in version 1, to use web container as a background view component, you had to first set the
$jason.body.background.type
to 'html'
and then hard-code the HTML text under $jason.body.background.text
attribute like this:Naturally people wanted to be able to instantiate the container using simply a web URL instead of having to hardcode the entire HTML text in a single line.
Solution
Web container 2.0 has added the
url
attribute. You can embed a local file://
HTML like this (it loads from the local HTML file you ship with the app):Or embed a remote
http[s]://
URL like this (it loads from a remote HTML):2. Parent App <=> Web Container Communication
Problem
Previously, web containers were only for displaying content, and not interactive. This meant NONE of the following was possible:
- Jasonette => Web Container: Call JavaScript functions inside the web container from Jasonette.
- Web Container => Jasonette: Call native API from web container code.
All you could do was display the web container. This was similar to how you would embed an iframe in a web page, but the main web page had no access to what was inside the iframe.
Coherence 6 0 – Turn Websites Into Apps Free
Solution
The whole point of Jasonette is to design a standard markup language to describe cross platform mobile apps. In this case, we needed a markup language that could comprehensively describe communications between the parent app and the child web container.
To achieve this, I came up with a
JSON-RPC
based communication channel between the parent app and the child web container. Since everything on Jasonette is expressed in JSON objects, it made perfect sense to use the JSON-RPC standard format as the communication protocol.To make a JavaScript function call into the web container, we declare an action called
$agent.request
:$agent.request
is the native API that triggers a JSON-RPC request into the web container. To use it, we must pass an options
object as its parameter.The
options
object is the actual JSON-RPC request that will be sent to the web container. Let’s look at what each attribute means:id
: Web container is built on top of a lower level architecture called agent. Normally you can have multiple agents for a single view, and each agent can have its unique ID. But Web container is a special type of agent which can only have the id of$webcontainer
, which is why we use that ID here.method
: The JavaScript function name to callparams
: The array of parameters to pass to the JavaScript function.
The full markup would look something like this:
This markup is saying:
When the view loads (
$jason.head.actions.$load
), make a JSON-RPC request into the web container agent ($agent.request
) where the request is specified under options
.The web container is defined under
$jason.body.background
, which in this case loads a local file called file://index.html
.It will look for a JavaScript function called
login
and pass the two arguments under params
( 'alice'
and '1234'
)I’ve only explained how the parent app can trigger the child web container’s JavaScript function calls, but you can also do the opposite and let the web container trigger the parent app’s native API.
To learn more, check out the agent documentation.
Example
Let’s come back to the QR code example I briefly shared above:
- The footer input component is 100% native.
- The QR code is generated by the web container as a web app.
- When a user enters something and presses “Generate,” it calls
$agent.request
action into the web container agent, calling the JavaScript function “qr”
You can check out the example here.
3. Script Injection
Problem
Sometimes you may want to dynamically inject JavaScript code into the web container AFTER it’s finished loading the initial HTML.
Imagine you want to build a custom web browser app. You may want to inject your own custom JavaScript into every web view to customize the web view’s behavior, kind of like how web browser extensions work.
Even if you’re not building a web browser, you may want to use the script injection method whenever you want a custom behavior for a URL whose content you have no control over. The only way to communicate between the native app and the web container is through the
$agent
API. But if you can’t change the HTML content, the only way to add the $agent
interface into the web container is through dynamic injection.Solution
As mentioned in the previous section, the
$jason.body.background
web container is just another agent
. This means you can use the same $agent.inject
method available to regular agents.4. URL Click Handling
In the past, there were only two ways a web container could handle link clicks:
- Readonly: Treat the web container as readonly and ignore all events such as touch or scroll. All web containers are readonly unless you tell them to behave like a regular browser, as described below.
- Regular Browser Behavior: Let users interact with the page by behaving like a normal browser. You declare it by setting
'type': '$default'
as itsaction
attribute.
Problem
Both are “all or nothing” solutions.
- In the “Readonly” case, all your interactions are completely ignored by the web container.
- In the “Regular Browser Behavior” case, the web container functions literally as a browser. When you click a link, it would just send you to that link by refreshing the page just like a web page. There was no way to hijack the click and call some native API.
Solution
With the new web container, you can now attach any
action
on the $jason.body.background
web container to handle link click events.Let’s look at an example:
Here we have attached
'trigger': 'displayBanner'
to the web container. This means that when a user clicks any link in the web container, it will trigger displayBanner
action instead of letting the web view handle it.Also, if you look at the
displayBanner
action, you’ll notice the $jason
variable. In this case, the clicked link will be passed through the $jason
variable. For example, if you clicked a URL named 'https://google.com'
, the $jason
will have the following value:This means you can selectively trigger different actions by checking the
$jason.url
value.Let’s take another example where we implement a custom web browser:
We test if the URL contains the string
signin
and then run two different actions depending on the result.- If it contains
signin
, it opens a new view to take care of signing in natively. - If it doesn’t contain
signin
, just run the'type': '$default'
action so that it behaves like a regular browser.
Example Usage
Building a custom web browser
We can now take advantage of the fact that the new web container can:
- Take a
url
attribute to load itself, functioning as a full-fledged browser - Selectively handle link clicks depending on the URL
We can even build a custom web browser app with just a dozen lines of JSON. Since we can now hijack every link click, we can take a look at
$jason.url
and run whatever actions we want depending on the URL.For example, take a look at the example below:
On the left side we see that clicking a link behaves like a regular browser (
'type': '$default'
)On the right side we see that clicking a link does a native transition to another JASON view.
All this can be achieved by selectively triggering different actions based on
$jason.url
.Step 1. Attach an action named
visit
to the web container like this:Step 2. Run relevant actions inside
visit,
based on $jason.url
In the following code, we’re checking if
$jason.url
matches newest
, show
, ask
, and so on (they’re the top menu item links). If they do, we let the web container behave like a regular browser by setting 'type': '$default'
If they don’t match the pattern, we make a native
$href
transition to a new view and pass the clicked link as a parameter.Check out the full JSON markup for the web browser here (it’s only 48 lines!).
Instant “Hybrid” App
When people normally talk about “hybrid” apps, they mostly mean HTML web apps wrapped inside a native app frame.
But that’s not what I mean here. When I say “Hybrid,” I mean a truly hybrid app, where one app can have multiple native views and multiple web-based views simultaneously. Also where one view can have multiple native UI components and a web container rendered in the same native layout.
The cross-over between web-based view and native view should be so seamless that it’s hard to tell where one starts and ends.
In this example, I’ve created an app that displays jasonbase.com in a web container as the home view.
Jasonbase is a free JSON hosting service I built to easily host JSON markup for Jasonette apps.
Naturally, it’s just a website, but I have embedded it in Jasonette so that when you click the link, instead of opening a web page, it makes a native
$href
transition to a native JASON view.I didn’t have to touch any of Jasonbase.com’s code to build this app.
I simply embedded the website into Jasonette as a web container, and hijacked the link clicks to handle them natively, so it can do all the native stuff like triggering native APIs and making native transitions.
You can check out the code here.
Coherence 6 0 – Turn Websites Into Apps Using
Conclusion
In my opinion, what makes all this work fabulously is that everything is taken care of on the framework level. All the hard work is taken care of behind the scenes.
Instead of putting the burden on the app developers to implement all of the following from scratch:
![Coherence 6 0 – turn websites into apps without Coherence 6 0 – turn websites into apps without](https://static.wixstatic.com/media/598d4a_b2990c7147f44ed093190d6ba58c9d13~mv2_d_1344_1582_s_2.png/v1/fill/w_512,h_603,al_c,usm_0.66_1.00_0.01/Unite%202019-12-08%20at%2013_28_10_2x.png)
- Embed a webview into native layout
- Create a JavaScript bridge so the app can make function calls into the web view
- Creating a native event handling architecture so the web view can trigger native events on the parent app
Coherence 6 0 – Turn Websites Into Apps Pdf
The solution was to create an abstraction made up of:
- Declarative Markup Language: for describing how to embed a web view into a native app
- Communication Protocol (JSON-RPC): to allow dead-simple interactions between the app and its child web views.
I don’t claim this approach to be the ultimate solution to solve everything, but I’m happy to say that this has been a great solution for my own use case.
I was trying to build an app that builds on a super edge technology which has no stable and reliable mobile implementations (and it’s not clear if there ever will be a mobile implementation due to the protocol’s nature). Thankfully it had JavaScript implementations so I could easily integrate it into the app without hassle.
Overall, it’s been great and I’m satisfied with how it turned out. The documentation is up to date to reflect all the new features, so feel free to dig in and play around.
Disclaimer: With great power comes great responsibility
I would like to end with a disclaimer: as great as this newly found power is, I think you need to keep a balance to build an app with a great user experience.
Some may take this and build an entire app using web views only, but then you will end up with an app that’s basically just a website, which defeats the purpose of building a dedicated app.
I emphasize that I’m not saying you should always build apps with both HTML and native. I am saying this can be very useful for many people in different situations. Just don’t go overboard with it.
Follow Along to Learn More
Coherence 6 0 – Turn Websites Into Apps Without
There are many different configurations in which the Jasonette native core and its child web container can communicate to get things done in creative and powerful ways, and this post is just scratching the surface.
Going forward I’m planning to share more of these use cases and tutorials, so if you’re interested, please follow along on medium or twitter.