Web modules are exclusive to Velo and enable you to write functions that run server-side in the backend, and easily call them in your client-side code. With web modules you can import functions from the backend into files or scripts in page code or public files, knowing they will run server-side in the backend. Velo handles all the client-server communication required to enable this access. See the advanced tip below if you want to know how this communication is handled.
When you import a web module on the client-side you get a proxy function to the web module function. This proxy function uses an XMLHttpRequest to invoke the function in the backend. The runtime listens to those invocations and calls the appropriate function.
The arguments and return value are serialized using JSON.
The following example shows how you can call a backend function in your client-side code:
Copy Code
import { rotatingGreeting } from 'backend/helloModule';$w.onReady(function () {let callCount = 0;$w('#button').onClick(async () => {$w('#responseText').text = await rotatingGreeting(callCount++);$w('#responseText').show();});});
And here is the above code in action:
You can experiment with this example for yourself on our Hello Web Modules example page.
Web module functions contain code you would typically want or need to run in the backend. Your code may have security risks if it runs in the frontend, or you may want to access other web services. For example, let's say you want to enable your site visitor to send an email via a 3rd-party provider. You would store your API key to the 3rd-party service in the Secrets Manager, and write your function that sends the email in the backend, like this:
Copy Code
// Filename: backend/sendEmail.jsw (web modules need to have a *.jsw* extension)import {getSecret} from 'wix-secrets-backend';import {fetch} from 'wix-fetch';// wix-fetch is the API we provide to make https calls in the backendconst API_KEY = await getSecret(<SECRET_NAME>);export function sendEmail (address, subject, body) {return fetch("https://a-backend-service-that-sends-email.com/send?APIKey=" + API_KEY, {method: 'post',body: JSON.stringify({address, subject, body})}).then(function(response) {if (response.status >= 200 && response.status < 300)return response.text();elsethrow new Error(response.statusText);});};
You need this code to run in the backend for two reasons:
You would then import the function from your web module into your frontend file and use it:
Copy Code
import {sendEmail} from 'backend/sendEmail.jsw';export function sendButton_onClick(event) {sendEmail($w("#addressInput").value,$w("#subjectInput").value,$w("#bodyInput").value).then(function() {console.log("email was sent");});}
You can import a web module function into another module in the backend.
Unlike regular modules that allow you to export functions, objects, and other items, you can only export functions from web modules. Web modules also always return a promise. This is true even if, in the implementation of the function, it returns a value. For example, if your web module function returns a value, like this:
Copy Code
// Filename: aModule.jsw (web modules need to have a *.jsw* extension)export function multiply(factor1, factor2) {return factor1 * factor2;}
When you call the function, it still returns a promise that resolves to the value. So you need to use it like this:
Copy Code
import {multiply} from 'backend/aModule';multiply(4,5).then(function(product) {console.log(product);// Logs: 20});
You can log messages to the console in web modules and they will be displayed in the client's console log, even though the code is running in the backend.
Because Web Modules allow you to call your backend code from the frontend, it's important that you limit which visitors can access their functionality by setting their permissions. See About Web Module Permissions to learn more.