{"id":16200,"date":"2024-08-21T14:18:27","date_gmt":"2024-08-21T21:18:27","guid":{"rendered":"https:\/\/www.couchbase.com\/blog\/?p=16200"},"modified":"2024-08-27T09:51:59","modified_gmt":"2024-08-27T16:51:59","slug":"svelte-and-couchbase","status":"publish","type":"post","link":"https:\/\/www.couchbase.com\/blog\/svelte-and-couchbase\/","title":{"rendered":"Quick and In Sync with Svelte and Couchbase"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">Are you ready to build a reactive real-time web application?\u00a0<\/span><\/p>\n<p><i><span style=\"font-weight: 400;\">tl;dr Don\u2019t have time to read and work through this entire tutorial? You\u2019re in luck! You can find a fully working example on <\/span><\/i><a href=\"https:\/\/github.com\/hummusonrails\/real-time-chat-svelte-couchbase\"><i><span style=\"font-weight: 400;\">GitHub<\/span><\/i><\/a><i><span style=\"font-weight: 400;\">. Feel free to go there, clone the repository and start exploring.<\/span><\/i><\/p>\n<hr \/>\n<p><span style=\"font-weight: 400;\">In this tutorial, we&#8217;ll explore the combination of <\/span><a href=\"https:\/\/svelte.dev\/\"><span style=\"font-weight: 400;\">Svelte<\/span><\/a><span style=\"font-weight: 400;\"> and <\/span><a href=\"https:\/\/cloud.couchbase.com\/\"><span style=\"font-weight: 400;\">Couchbase Capella<\/span><\/a><span style=\"font-weight: 400;\"> to create a dynamic and interactive chat application. Svelte, with an impressive 72.8% admiration rate according to the <\/span><a href=\"https:\/\/survey.stackoverflow.co\/2024\/technology#admired-and-desired-webframe-desire-admire\"><span style=\"font-weight: 400;\">2024 Stack Overflow Developer Survey<\/span><\/a><span style=\"font-weight: 400;\">, is celebrated for several reasons. It efficiently takes much of the work out of the browser by compiling your components into highly efficient, imperative code that directly manipulates the DOM, eliminating the need for a virtual DOM and leading to faster updates and smaller bundle sizes.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Svelte&#8217;s built-in reactivity automatically tracks state changes, ensuring fast and efficient updates without the need for complex state management libraries. This reactivity simplifies the development process and enhances performance. Additionally, Svelte&#8217;s component-based architecture makes it easier to build and maintain complex user interfaces, offering a more straightforward and enjoyable development experience. And, when your application needs responsive, adaptive data Couchbase Capella offers a straightforward solution to implement.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Couchbase Capella is not just a NoSQL cloud database platform; it&#8217;s an all-in-one data platform offering full-text search, vector search, data caching, analytics, and more. This comprehensive functionality allows you to build robust applications with diverse data requirements. Together, Svelte and Couchbase Capella make possible real-time applications that are blazingly fast and performant.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Okay, enough talk. Let\u2019s get started!<\/span><\/p>\n<h2><span style=\"font-weight: 400;\">Setting up the Dev Environment<\/span><\/h2>\n<h3><span style=\"font-weight: 400;\">Creating a New SvelteKit Project<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">Before diving into the setup, let&#8217;s clarify the difference between Svelte and SvelteKit. Svelte is a front-end framework that compiles your code into highly efficient, imperative code that directly manipulates the DOM. This results in faster updates and smaller bundle sizes. On the other hand, SvelteKit is a framework built on top of Svelte, designed for building full-stack web applications. SvelteKit provides additional features such as routing, server-side rendering, and static site generation, making it a powerful tool for developing modern web applications.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">In your project, SvelteKit will handle the application structure, routing, and server-side rendering, while Svelte will manage the efficient rendering of UI components.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">To start a new project you can initialize it on the command line:<\/span><\/p>\n<pre class=\"nums:false lang:default decode:true\">npm create svelte@latest svelte-couchbase-real-time-chat\r\ncd svelte-couchbase-real-time-chat\r\n<\/pre>\n<p><span style=\"font-weight: 400;\">You will be prompted with several choices by the CLI tool. Answer with the following responses:<\/span><\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Skeleton Project<\/b><span style=\"font-weight: 400;\">: Select <\/span><strong>yes<\/strong><span style=\"font-weight: 400;\"> to start with a basic project structure.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Typescript<\/b><span style=\"font-weight: 400;\">: Select <\/span><strong>no<\/strong><span style=\"font-weight: 400;\"> for now to keep things simple.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>ESLint<\/b><span style=\"font-weight: 400;\">: Select <\/span><strong>yes<\/strong><span style=\"font-weight: 400;\"> to include linting, which helps in maintaining code quality.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Prettier<\/b><span style=\"font-weight: 400;\">: Select <\/span><strong>yes<\/strong><span style=\"font-weight: 400;\"> for code formatting.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Playwright<\/b><span style=\"font-weight: 400;\">: Select <\/span><strong>no<\/strong><span style=\"font-weight: 400;\"> unless you plan to write end-to-end tests from the beginning.<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Then, install the dependencies by running <\/span><em><span style=\"font-weight: 400;\">npm install<\/span><\/em><span style=\"font-weight: 400;\"> from the command line. After running these commands, you&#8217;ll have a new SvelteKit project set up and ready to go!<\/span><\/p>\n<h3><span style=\"font-weight: 400;\">Installing Dependencies<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">Next, we&#8217;ll install additional dependencies required for our project, including Tailwind CSS for styling, the Couchbase SDK for database interactions, and WebSocket support for real-time communication.<\/span><\/p>\n<pre class=\"nums:false lang:default decode:true \">npm install -D tailwindcss postcss autoprefixer couchbase ws dotenv\r\nnpx tailwindcss init -p<\/pre>\n<p><span style=\"font-weight: 400;\">What are each of these dependencies doing in your application?<\/span><\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><a href=\"https:\/\/tailwindcss.com\/\"><span style=\"font-weight: 400;\">TailwindCSS<\/span><\/a><span style=\"font-weight: 400;\"> is a utility-first CSS framework that makes it more straightforward to style your application with predefined classes.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">The <\/span><a href=\"https:\/\/docs.couchbase.com\/nodejs-sdk\/current\/hello-world\/start-using-sdk.html\"><span style=\"font-weight: 400;\">Couchbase Node.js SDK<\/span><\/a><span style=\"font-weight: 400;\"> introduces functionality to interact with your data on Couchbase Capella using methods that abstract away a lot of the work.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><em><span style=\"font-weight: 400;\">ws<\/span><\/em><span style=\"font-weight: 400;\"> is a small Websocket library for Node.js. You will be enabling the real-time chat functionality in the application with Websockets.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Lastly, <\/span><em><span style=\"font-weight: 400;\">dotenv<\/span><\/em><span style=\"font-weight: 400;\"> loads environment variables from a <\/span><i><span style=\"font-weight: 400;\">.env<\/span><\/i><span style=\"font-weight: 400;\"> file into <\/span><em><span style=\"font-weight: 400;\">process.env<\/span><\/em><span style=\"font-weight: 400;\"> making it easier to manage configuration.<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3><span style=\"font-weight: 400;\">Configuring TailwindCSS<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">As mentioned above, TailwindCSS introduces classes that you can use to define the styling for your application. This is helpful if you are not a frontend expert, or even if you are, if you wish to shortcut the process of building elegantly designed applications. To use TailwindCSS in your Svelte project, follow these steps:<\/span><\/p>\n<h4><span style=\"font-weight: 400;\">Configure <\/span><em><span style=\"font-weight: 400;\">tailwind.config.js<\/span><\/em><\/h4>\n<p><span style=\"font-weight: 400;\">Update the <\/span><em><span style=\"font-weight: 400;\">tailwind.config.cjs<\/span><\/em><span style=\"font-weight: 400;\"> file to specify the content sources. This ensures that Tailwind CSS can remove unused styles from your production build, making it more efficient.<\/span><\/p>\n<pre class=\"nums:false lang:default decode:true\">\/** @type {import('tailwindcss').Config} *\/\r\nmodule.exports = {\r\n\u00a0\u00a0content: ['.\/src\/**\/*.{html,js,svelte,ts}'],\r\n\u00a0\u00a0theme: {\r\n\u00a0\u00a0\u00a0\u00a0extend: {},\r\n\u00a0\u00a0},\r\n\u00a0\u00a0plugins: [],\r\n}\r\n<\/pre>\n<h4><span style=\"font-weight: 400;\">Add Tailwind CSS directives<\/span><\/h4>\n<p><span style=\"font-weight: 400;\">Create or update the <\/span><em><span style=\"font-weight: 400;\">src\/app.css<\/span><\/em><span style=\"font-weight: 400;\"> file to include Tailwind CSS directives. These directives load Tailwind&#8217;s base, components, and utility styles.<\/span><\/p>\n<pre class=\"nums:false lang:default decode:true\">@tailwind base;\r\n@tailwind components;\r\n@tailwind utilities;<\/pre>\n<h4><span style=\"font-weight: 400;\">Include the CSS in your Svelte project<\/span><\/h4>\n<p><span style=\"font-weight: 400;\">Open or create the <\/span><em><span style=\"font-weight: 400;\">src\/routes\/+layout.svelte<\/span><\/em><span style=\"font-weight: 400;\"> file and import the CSS file. This ensures that Tailwind&#8217;s styles are available throughout your application.<\/span><\/p>\n<pre class=\"nums:false lang:default decode:true\">&lt;script&gt;\r\n\u00a0\u00a0import \"..\/app.css\";\r\n&lt;\/script&gt;\r\n\r\n&lt;slot \/&gt;\r\n<\/pre>\n<p><span style=\"font-weight: 400;\">Now that you\u2019ve completed these steps, TailwindCSS has been successfully initialized in your application! You&#8217;re ready to move on to setting up Couchbase Capella and building the backend for your chat application.\u00a0<\/span><\/p>\n<h2><span style=\"font-weight: 400;\">Setting Up Couchbase Capella<\/span><\/h2>\n<h3><span style=\"font-weight: 400;\">Creating a Cluster<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">It is free to sign up and try Couchbase Capella, and if you have not done so yet, you can do so by navigating to <\/span><a href=\"https:\/\/cloud.couchbase.com\/\"><span style=\"font-weight: 400;\">cloud.couchbase.com<\/span><\/a><span style=\"font-weight: 400;\"> and creating an account using your GitHub or Google credentials, or by making a new account with an email address and password combination.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Once you have done so, from within your Capella dashboard, you will create your first cluster. For the purposes of this walkthrough, let\u2019s name it <\/span><i><span style=\"font-weight: 400;\">SvelteChatApp<\/span><\/i><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The summary of your new cluster will be presented on the left-hand side of the dashboard. Capella is multi-cloud and can work with AWS, Google Cloud or Azure. For this example, you will deploy to AWS.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">After you have created your cluster, you need to create a <\/span><i><span style=\"font-weight: 400;\">bucket<\/span><\/i><span style=\"font-weight: 400;\">. A <\/span><i><span style=\"font-weight: 400;\">bucket<\/span><\/i><span style=\"font-weight: 400;\"> in Couchbase is the container where the data is stored. Each item of data, known as a document, is kept in JSON making its syntax familiar to most developers. You can name your bucket whatever you want. However, for the purposes of this walkthrough, let\u2019s name this bucket <\/span><i><span style=\"font-weight: 400;\">svelte_chat_app_messages<\/span><\/i><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Now that you have created both your database and your bucket, you are ready to create your database access credentials and to fetch your connection URL that you will be using in your Lambda function.<\/span><\/p>\n<h3><span style=\"font-weight: 400;\">Obtaining Connection Details<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">The connection details are essential as you will be using them in your application to establish a connection to your Couchbase data and to interact with the data. Navigate to the <\/span><i><span style=\"font-weight: 400;\">Connect<\/span><\/i><span style=\"font-weight: 400;\"> section in the Capella dashboard and take note of the <\/span><i><span style=\"font-weight: 400;\">Connection String<\/span><\/i><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><a href=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2024\/08\/image2-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-16201\" style=\"border: 1px solid black;\" src=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2024\/08\/image2-2-1024x583.png\" alt=\"Capella SDK connection details\" width=\"900\" height=\"512\" srcset=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2024\/08\/image2-2-1024x583.png 1024w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2024\/08\/image2-2-300x171.png 300w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2024\/08\/image2-2-768x438.png 768w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2024\/08\/image2-2-1536x875.png 1536w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2024\/08\/image2-2-1320x752.png 1320w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2024\/08\/image2-2.png 1999w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">Then, click on the <\/span><i><span style=\"font-weight: 400;\">Database Access<\/span><\/i><span style=\"font-weight: 400;\"> link under section two. In that section, you will create credentials \u2013 a username and password \u2013 that your application will use to authenticate with the database. You can scope the credentials to the specific bucket you created or give it permission for all buckets and databases in your account. You need to make sure it has both read and write access, regardless.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Once you have finished, the last step in this part of the process is to add your new connection string and connection credentials to your application as environment variables.<\/span><\/p>\n<h3><span style=\"font-weight: 400;\">Setting Up Environment Variables<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">In a production environment, you will store your credentials and other confidential information for your application in a secure format. Different cloud providers have different paths to store sensitive information, and you should follow the procedure defined by the cloud provider you are using, whether it is AWS, Google Cloud, Azure, Netlify, Vercel or any other. For our purposes, you are adding your credentials to a <\/span><i><span style=\"font-weight: 400;\">.env<\/span><\/i><span style=\"font-weight: 400;\"> file in the root folder of your application. The <\/span><em><span style=\"font-weight: 400;\">dotenv<\/span><\/em><span style=\"font-weight: 400;\"> package reads those credentials from there and loads them into your application.<\/span><\/p>\n<pre class=\"nums:false lang:default decode:true\"># .env\r\nCOUCHBASE_BUCKET=your_bucket_name\r\nCOUCHBASE_CONNECTION_STRING=your_connection_string\r\nCOUCHBASE_USER=your_username\r\nCOUCHBASE_PASSWORD=your_password<\/pre>\n<p><span style=\"font-weight: 400;\">That\u2019s it! Your Couchbase cluster is all set up and ready to be used. At this point, you are ready to build the application. Let\u2019s start with the backend server with Nodejs and then move on to the frontend with Svelte.<\/span><\/p>\n<h2><span style=\"font-weight: 400;\">Building the Backend with Node.js<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">With our development environment set up, it&#8217;s time to build the backend for our real-time chat application. We&#8217;ll use Node.js to create the server, connect to Couchbase Capella for data storage, and set up a WebSocket server for real-time communication.<\/span><\/p>\n<h3><span style=\"font-weight: 400;\">Creating <\/span><em><span style=\"font-weight: 400;\">server.cjs<\/span><\/em><\/h3>\n<p><span style=\"font-weight: 400;\">First, we&#8217;ll create a file named server.cjs which will serve as the entry point for our backend.<\/span><\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Create <\/span><em><span style=\"font-weight: 400;\">server.cjs<\/span><\/em><span style=\"font-weight: 400;\">:<br \/>\nIn the root directory of your project, create a new file named <i>server.cjs<\/i>.<br \/>\n<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Setup Express and Dependencies:<br \/>\nWe&#8217;ll use Express for our server framework, the Couchbase SDK for database operations, WebSocket for real-time communication, and <em>dotenv<\/em> for environment variables management. Add the following code to <i>server.cjs<\/i>:<br \/>\n<\/span><\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<pre class=\"nums:false lang:default decode:true\">const express = require('express');\r\nconst couchbase = require('couchbase');\r\nconst { createServer } = require('http');\r\nconst { WebSocketServer } = require('ws');\r\nconst dotenv = require('dotenv');\r\n\r\ndotenv.config();\r\n\r\nconst app = express();\r\nconst server = createServer(app);\r\nconst wss = new WebSocketServer({ server });<\/pre>\n<h3><span style=\"font-weight: 400;\">Connecting to Couchbase<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">Next, we&#8217;ll set up the connection to Couchbase Capella. Ensure your .env file contains the correct connection details. Add the following code to <\/span><i><span style=\"font-weight: 400;\">server.cjs <\/span><\/i><span style=\"font-weight: 400;\">\u00a0to connect to Couchbase:<\/span><\/p>\n<pre class=\"nums:false lang:default decode:true\">let cluster, bucket, collection;\r\n\r\nasync function connectToCouchbase() {\r\n\u00a0\u00a0\u00a0\u00a0try {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0console.log('Connecting to Couchbase...');\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0const clusterConnStr = process.env.COUCHBASE_CONNECTION_STRING;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0const username = process.env.COUCHBASE_USER;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0const password = process.env.COUCHBASE_PASSWORD;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0const bucketName = process.env.COUCHBASE_BUCKET;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0cluster = await couchbase.connect(clusterConnStr, {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0username: username,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0password: password,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0configProfile: 'wanDevelopment',\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0});\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0bucket = cluster.bucket(bucketName);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0collection = bucket.defaultCollection();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0console.log('Connected to Couchbase successfully.');\r\n\u00a0\u00a0\u00a0\u00a0} catch (error) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0console.error('Error connecting to Couchbase:', error);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0process.exit(1);\r\n\u00a0\u00a0\u00a0\u00a0}\r\n}\r\n\r\nconnectToCouchbase();<\/pre>\n<p><span style=\"font-weight: 400;\">This function handles the connection to Couchbase, ensuring that all necessary parameters are properly configured. All that is left for our backend is to create the websocket server to handle the sending and receiving of new chat messages.<\/span><\/p>\n<h3><span style=\"font-weight: 400;\">Setting up the Websocket Server<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">The Websocket server functionality is also added to the <\/span><i><span style=\"font-weight: 400;\">server.cjs<\/span><\/i><span style=\"font-weight: 400;\"> file. The server will broadcast all new messages for the frontend of the application to receive, and send all newly created messages to Couchbase for saving in the bucket you created.<\/span><\/p>\n<pre class=\"nums:false lang:default decode:true\">wss.on('connection', (ws) =&gt; {\r\n\u00a0\u00a0\u00a0\u00a0console.log('New WebSocket connection established.');\r\n\u00a0\u00a0\u00a0\u00a0ws.on('message', async (message) =&gt; {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0try {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0const messageString = message.toString();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0console.log('Received message:', messageString);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ Save message to Couchbase\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0const id = `message::${Date.now()}`;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0await collection.upsert(id, { text: messageString });\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0console.log('Message saved to Couchbase:', id);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ Broadcast message\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0wss.clients.forEach((client) =&gt; {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (client.readyState === WebSocket.OPEN) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0client.send(messageString);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0console.log('Broadcasted message to client:', messageString);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0});\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0} catch (error) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0console.error('Error handling message:', error);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0});\r\n});\r\n\r\nserver.listen(3000, () =&gt; {\r\n\u00a0\u00a0\u00a0\u00a0console.log('Server started on https:\/\/localhost:3000');\r\n});<\/pre>\n<p><span style=\"font-weight: 400;\">Note that before sending the message to Couchbase, you first convert the message into a <\/span><em><span style=\"font-weight: 400;\">String<\/span><\/em><span style=\"font-weight: 400;\"> as it is received as binary data buffers by default. The conversion to <\/span><em><span style=\"font-weight: 400;\">String<\/span><\/em><span style=\"font-weight: 400;\"> format is achieved by calling the <\/span><em><span style=\"font-weight: 400;\">toString()<\/span><\/em><span style=\"font-weight: 400;\"> function on the message. The newly defined <\/span><em><span style=\"font-weight: 400;\">messageString<\/span><\/em><span style=\"font-weight: 400;\"> variable now contains the data in readable format for both sending to Couchbase and rendering in the application.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">That is the entire backend of your new real-time chat application. However, as good as any backend for a web application is, it needs a frontend to render it for the user. Svelte offers us the performance and reactivity to do so with speed and with an excellent developer experience.\u00a0<\/span><\/p>\n<h2><span style=\"font-weight: 400;\">Creating the Frontend with Svelte<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">With your backend set up, it&#8217;s time to build the frontend of our real-time chat application using Svelte. You\u2019ll leverage Svelte&#8217;s strengths to create a responsive and dynamic chat interface.<\/span><\/p>\n<h3><span style=\"font-weight: 400;\">Building the Chat Interface<\/span><\/h3>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Create the Svelte Component<br \/>\nIn your SvelteKit project, create a new file named <i>+page.svelte<\/i> in the <i>src\/routes<\/i> directory. This file will serve as the main component for our chat interface.<br \/>\n<\/span><\/span><\/p>\n<pre class=\"nums:false lang:default decode:true\">touch src\/routes\/+page.svelte<\/pre>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Set Up the Basic Structure<br \/>\n<\/span>Open <i>+page.svelte<\/i> and set up the basic HTML structure with TailwindCSS classes for styling:<\/p>\n<pre class=\"nums:false lang:default decode:true\">&lt;script&gt;\r\n\u00a0\u00a0import { onMount } from 'svelte';\r\n\r\n\u00a0\u00a0let messages = [];\r\n\u00a0\u00a0let newMessage = '';\r\n\r\n\u00a0\u00a0let ws;\r\n\r\n\u00a0\u00a0onMount(() =&gt; {\r\n\u00a0\u00a0\u00a0\u00a0ws = new WebSocket('ws:\/\/localhost:3000');\r\n\r\n\u00a0\u00a0\u00a0\u00a0ws.onmessage = (event) =&gt; {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0messages = [...messages, event.data];\r\n\u00a0\u00a0\u00a0\u00a0};\r\n\r\n\u00a0\u00a0\u00a0\u00a0ws.onopen = () =&gt; {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0console.log('WebSocket connection opened');\r\n\u00a0\u00a0\u00a0\u00a0};\r\n\r\n\u00a0\u00a0\u00a0\u00a0ws.onclose = () =&gt; {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0console.log('WebSocket connection closed');\r\n\u00a0\u00a0\u00a0\u00a0};\r\n\u00a0\u00a0});\r\n\r\n\u00a0\u00a0function sendMessage() {\r\n\u00a0\u00a0\u00a0\u00a0ws.send(newMessage);\r\n\u00a0\u00a0\u00a0\u00a0newMessage = '';\r\n\u00a0\u00a0}\r\n&lt;\/script&gt;\r\n\r\n&lt;div class=\"container mx-auto p-4\"&gt;\r\n\u00a0\u00a0&lt;h1 class=\"text-2xl mb-4\"&gt;Chat Application&lt;\/h1&gt;\r\n\u00a0\u00a0&lt;div class=\"border p-4 mb-4 h-64 overflow-y-scroll\"&gt;\r\n\u00a0\u00a0\u00a0\u00a0{#each messages as message}\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;div&gt;{message}&lt;\/div&gt;\r\n\u00a0\u00a0\u00a0\u00a0{\/each}\r\n\u00a0\u00a0&lt;\/div&gt;\r\n\u00a0\u00a0&lt;input\r\n\u00a0\u00a0\u00a0\u00a0type=\"text\"\r\n\u00a0\u00a0\u00a0\u00a0bind:value={newMessage}\r\n\u00a0\u00a0\u00a0\u00a0class=\"border p-2 w-full mb-2\"\r\n\u00a0\u00a0\u00a0\u00a0placeholder=\"Type a message\"\r\n\u00a0\u00a0\/&gt;\r\n\u00a0\u00a0&lt;button on:click={sendMessage} class=\"bg-blue-500 text-white p-2 w-full\"&gt;Send&lt;\/button&gt;\r\n&lt;\/div&gt;\r\n<\/pre>\n<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p style=\"padding-left: 40px;\"><span style=\"font-weight: 400;\">The <\/span><em><span style=\"font-weight: 400;\">&lt;script&gt;<\/span><\/em><span style=\"font-weight: 400;\"> section of the above code example initializes Websocket and handles the messages, both sending and receiving. The <\/span><em><span style=\"font-weight: 400;\">onMount<\/span><\/em><span style=\"font-weight: 400;\"> function ensures that the Websocket connection is established when the component is initialized. Svelte&#8217;s reactivity automatically updates the DOM whenever the <\/span><em><span style=\"font-weight: 400;\">messages<\/span><\/em><span style=\"font-weight: 400;\"> array changes, ensuring new messages are displayed in real-time. With that your frontend is now complete.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">You did it! You have built an entire chat application enabling real-time communication utilizing the performance, flexibility and adaptability of both Svelte and Couchbase to deliver an optimal experience for your users. Yes, this is a fairly simple implementation, however, it provides the skeleton for you to build even more expansive and complex real-time applications. The potential is only limited by your imagination.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Want to give it a go? Let\u2019s start up your application and begin chatting.<\/span><\/p>\n<h2><span style=\"font-weight: 400;\">Running the application<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">To run your application, you will initialize both the backend Node.js server and the SvelteKit frontend. First, let\u2019s start the backend from your terminal:<\/span><\/p>\n<pre class=\"nums:false lang:default decode:true\">node server.cjs<\/pre>\n<p><span style=\"font-weight: 400;\">Then, start the frontend in a new terminal window:<\/span><\/p>\n<pre class=\"nums:false lang:default decode:true\">npm run dev<\/pre>\n<p><span style=\"font-weight: 400;\">Now, navigate in your browser to <\/span><a href=\"https:\/\/localhost:5173\/\"><i><span style=\"font-weight: 400;\">https:\/\/localhost:5173<\/span><\/i><\/a><span style=\"font-weight: 400;\"> and start chatting!\u00a0<\/span><\/p>\n<p><a href=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2024\/08\/image3-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-16202\" style=\"border: 1px solid black;\" src=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2024\/08\/image3-2-1024x396.png\" alt=\"backend Node.js server and the SvelteKit frontend.\" width=\"900\" height=\"348\" srcset=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2024\/08\/image3-2-1024x396.png 1024w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2024\/08\/image3-2-300x116.png 300w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2024\/08\/image3-2-768x297.png 768w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2024\/08\/image3-2-1536x594.png 1536w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2024\/08\/image3-2-1320x510.png 1320w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2024\/08\/image3-2.png 1999w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">You can open up multiple browser tabs to simulate multiple users or use a service like <\/span><a href=\"https:\/\/ngrok.com\/\"><span style=\"font-weight: 400;\">ngrok<\/span><\/a><span style=\"font-weight: 400;\"> to share the application with your friends and chat in real time with them.<\/span><\/p>\n<h2><span style=\"font-weight: 400;\">Wrapping Up<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">In this tutorial you have learned how quickly you can create a fully responsive application that functions with real time data. Svelte updates the DOM with ease, while Couchbase lets you start creating and storing messages in mere seconds.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">There are so many reasons why Svelte is quickly growing in admiration and popularity in the highly competitive web framework space. Couchbase as the data backend paired with Svelte increases the potential for what you can build and achieve by so much more. No need to define complicated schema, no need to add more dependencies later on if you wish to implement data caching or search capabilities, it\u2019s all baked in with Couchbase and comes right out of the box.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The only remaining question is: What will you build next?<\/span><\/p>\n<p>Get started for free using <a href=\"https:\/\/cloud.couchbase.com\/\">Couchbase Capella<\/a>.<\/p>\n<p><br style=\"font-weight: 400;\" \/><br style=\"font-weight: 400;\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Are you ready to build a reactive real-time web application?\u00a0 tl;dr Don\u2019t have time to read and work through this entire tutorial? You\u2019re in luck! You can find a fully working example on GitHub. Feel free to go there, clone [&hellip;]<\/p>\n","protected":false},"author":85356,"featured_media":16207,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[1815,2242,2225,1822],"tags":[10009,1782],"ppma_author":[9985],"class_list":["post-16200","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-best-practices-and-tutorials","category-connectors","category-cloud","category-node-js","tag-svelte","tag-websocket"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v25.8 (Yoast SEO v25.8) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Quick and In Sync with Svelte and Couchbase - The Couchbase Blog<\/title>\n<meta name=\"description\" content=\"Build a chat application enabling real-time communication with the performance, flexibility and adaptability of both Svelte and Couchbase\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.couchbase.com\/blog\/svelte-and-couchbase\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Quick and In Sync with Svelte and Couchbase\" \/>\n<meta property=\"og:description\" content=\"Build a chat application enabling real-time communication with the performance, flexibility and adaptability of both Svelte and Couchbase\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.couchbase.com\/blog\/svelte-and-couchbase\/\" \/>\n<meta property=\"og:site_name\" content=\"The Couchbase Blog\" \/>\n<meta property=\"article:published_time\" content=\"2024-08-21T21:18:27+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-08-27T16:51:59+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2024\/08\/blog-svelta.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1256\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Ben Greenberg, Senior Developer Evangelist\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ben Greenberg, Senior Developer Evangelist\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/svelte-and-couchbase\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/svelte-and-couchbase\/\"},\"author\":{\"name\":\"Ben Greenberg, Senior Developer Evangelist\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/48efa1524aec97312d92f65a270c255d\"},\"headline\":\"Quick and In Sync with Svelte and Couchbase\",\"datePublished\":\"2024-08-21T21:18:27+00:00\",\"dateModified\":\"2024-08-27T16:51:59+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/svelte-and-couchbase\/\"},\"wordCount\":2103,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/svelte-and-couchbase\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2024\/08\/blog-svelta.png\",\"keywords\":[\"svelte\",\"websocket\"],\"articleSection\":[\"Best Practices and Tutorials\",\"Connectors\",\"Couchbase Capella\",\"Node.js\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.couchbase.com\/blog\/svelte-and-couchbase\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/svelte-and-couchbase\/\",\"url\":\"https:\/\/www.couchbase.com\/blog\/svelte-and-couchbase\/\",\"name\":\"Quick and In Sync with Svelte and Couchbase - The Couchbase Blog\",\"isPartOf\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/svelte-and-couchbase\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/svelte-and-couchbase\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2024\/08\/blog-svelta.png\",\"datePublished\":\"2024-08-21T21:18:27+00:00\",\"dateModified\":\"2024-08-27T16:51:59+00:00\",\"description\":\"Build a chat application enabling real-time communication with the performance, flexibility and adaptability of both Svelte and Couchbase\",\"breadcrumb\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/svelte-and-couchbase\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.couchbase.com\/blog\/svelte-and-couchbase\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/svelte-and-couchbase\/#primaryimage\",\"url\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2024\/08\/blog-svelta.png\",\"contentUrl\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2024\/08\/blog-svelta.png\",\"width\":2400,\"height\":1256,\"caption\":\"Build apps with Svelte and Couchbase\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/svelte-and-couchbase\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.couchbase.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Quick and In Sync with Svelte and Couchbase\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#website\",\"url\":\"https:\/\/www.couchbase.com\/blog\/\",\"name\":\"The Couchbase Blog\",\"description\":\"Couchbase, the NoSQL Database\",\"publisher\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.couchbase.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#organization\",\"name\":\"The Couchbase Blog\",\"url\":\"https:\/\/www.couchbase.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2023\/04\/admin-logo.png\",\"contentUrl\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2023\/04\/admin-logo.png\",\"width\":218,\"height\":34,\"caption\":\"The Couchbase Blog\"},\"image\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/48efa1524aec97312d92f65a270c255d\",\"name\":\"Ben Greenberg, Senior Developer Evangelist\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/image\/c9bda12524045d12a5878a2ef3fbe0de\",\"url\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2024\/06\/T024FJS4M-U075H3NTJUR-b4c321d902e2-512.jpeg\",\"contentUrl\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2024\/06\/T024FJS4M-U075H3NTJUR-b4c321d902e2-512.jpeg\",\"caption\":\"Ben Greenberg, Senior Developer Evangelist\"},\"url\":\"https:\/\/www.couchbase.com\/blog\/author\/bengreenberg\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Quick and In Sync with Svelte and Couchbase - The Couchbase Blog","description":"Build a chat application enabling real-time communication with the performance, flexibility and adaptability of both Svelte and Couchbase","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.couchbase.com\/blog\/svelte-and-couchbase\/","og_locale":"en_US","og_type":"article","og_title":"Quick and In Sync with Svelte and Couchbase","og_description":"Build a chat application enabling real-time communication with the performance, flexibility and adaptability of both Svelte and Couchbase","og_url":"https:\/\/www.couchbase.com\/blog\/svelte-and-couchbase\/","og_site_name":"The Couchbase Blog","article_published_time":"2024-08-21T21:18:27+00:00","article_modified_time":"2024-08-27T16:51:59+00:00","og_image":[{"width":2400,"height":1256,"url":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2024\/08\/blog-svelta.png","type":"image\/png"}],"author":"Ben Greenberg, Senior Developer Evangelist","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Ben Greenberg, Senior Developer Evangelist","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.couchbase.com\/blog\/svelte-and-couchbase\/#article","isPartOf":{"@id":"https:\/\/www.couchbase.com\/blog\/svelte-and-couchbase\/"},"author":{"name":"Ben Greenberg, Senior Developer Evangelist","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/48efa1524aec97312d92f65a270c255d"},"headline":"Quick and In Sync with Svelte and Couchbase","datePublished":"2024-08-21T21:18:27+00:00","dateModified":"2024-08-27T16:51:59+00:00","mainEntityOfPage":{"@id":"https:\/\/www.couchbase.com\/blog\/svelte-and-couchbase\/"},"wordCount":2103,"commentCount":0,"publisher":{"@id":"https:\/\/www.couchbase.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.couchbase.com\/blog\/svelte-and-couchbase\/#primaryimage"},"thumbnailUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2024\/08\/blog-svelta.png","keywords":["svelte","websocket"],"articleSection":["Best Practices and Tutorials","Connectors","Couchbase Capella","Node.js"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.couchbase.com\/blog\/svelte-and-couchbase\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.couchbase.com\/blog\/svelte-and-couchbase\/","url":"https:\/\/www.couchbase.com\/blog\/svelte-and-couchbase\/","name":"Quick and In Sync with Svelte and Couchbase - The Couchbase Blog","isPartOf":{"@id":"https:\/\/www.couchbase.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.couchbase.com\/blog\/svelte-and-couchbase\/#primaryimage"},"image":{"@id":"https:\/\/www.couchbase.com\/blog\/svelte-and-couchbase\/#primaryimage"},"thumbnailUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2024\/08\/blog-svelta.png","datePublished":"2024-08-21T21:18:27+00:00","dateModified":"2024-08-27T16:51:59+00:00","description":"Build a chat application enabling real-time communication with the performance, flexibility and adaptability of both Svelte and Couchbase","breadcrumb":{"@id":"https:\/\/www.couchbase.com\/blog\/svelte-and-couchbase\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.couchbase.com\/blog\/svelte-and-couchbase\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.couchbase.com\/blog\/svelte-and-couchbase\/#primaryimage","url":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2024\/08\/blog-svelta.png","contentUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2024\/08\/blog-svelta.png","width":2400,"height":1256,"caption":"Build apps with Svelte and Couchbase"},{"@type":"BreadcrumbList","@id":"https:\/\/www.couchbase.com\/blog\/svelte-and-couchbase\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.couchbase.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Quick and In Sync with Svelte and Couchbase"}]},{"@type":"WebSite","@id":"https:\/\/www.couchbase.com\/blog\/#website","url":"https:\/\/www.couchbase.com\/blog\/","name":"The Couchbase Blog","description":"Couchbase, the NoSQL Database","publisher":{"@id":"https:\/\/www.couchbase.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.couchbase.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.couchbase.com\/blog\/#organization","name":"The Couchbase Blog","url":"https:\/\/www.couchbase.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2023\/04\/admin-logo.png","contentUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2023\/04\/admin-logo.png","width":218,"height":34,"caption":"The Couchbase Blog"},"image":{"@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/48efa1524aec97312d92f65a270c255d","name":"Ben Greenberg, Senior Developer Evangelist","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/image\/c9bda12524045d12a5878a2ef3fbe0de","url":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2024\/06\/T024FJS4M-U075H3NTJUR-b4c321d902e2-512.jpeg","contentUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2024\/06\/T024FJS4M-U075H3NTJUR-b4c321d902e2-512.jpeg","caption":"Ben Greenberg, Senior Developer Evangelist"},"url":"https:\/\/www.couchbase.com\/blog\/author\/bengreenberg\/"}]}},"authors":[{"term_id":9985,"user_id":85356,"is_guest":0,"slug":"bengreenberg","display_name":"Ben Greenberg, Senior Developer Evangelist","avatar_url":{"url":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2024\/06\/T024FJS4M-U075H3NTJUR-b4c321d902e2-512.jpeg","url2x":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2024\/06\/T024FJS4M-U075H3NTJUR-b4c321d902e2-512.jpeg"},"author_category":"","last_name":"Greenberg, Senior Developer Evangelist","first_name":"Ben","job_title":"Senior Developer Evangelist","user_url":"","description":""}],"_links":{"self":[{"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/posts\/16200","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/users\/85356"}],"replies":[{"embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/comments?post=16200"}],"version-history":[{"count":0,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/posts\/16200\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/media\/16207"}],"wp:attachment":[{"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/media?parent=16200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/categories?post=16200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/tags?post=16200"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=16200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}