PageSourceSearch

https://sonar-docs.netlify.app/assets/js/3a25c827.cc725695.js

js sonar-docs.netlify.app collected 2026-10-03 11:26:11 UTC 9,865 bytes, 1 lines download raw bytes

1"use strict";(self.webpackChunkdocs=self.webpackChunkdocs||[]).push([[448],{3905:(e,n,t)=>{t.d(n,{Zo:()=>d,kt:()=>h});var a=t(7294);function r(e,n,t){return n in e?Object.defineProperty(e,n,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[n]=t,e}function o(e,n){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);n&&(a=a.filter((function(n){return Object.getOwnPropertyDescriptor(e,n).enumerable}))),t.push.apply(t,a)}return t}function s(e){for(var n=1;n<arguments.length;n++){var t=null!=arguments[n]?arguments[n]:{};n%2?o(Object(t),!0).forEach((function(n){r(e,n,t[n])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):o(Object(t)).forEach((function(n){Object.defineProperty(e,n,Object.getOwnPropertyDescriptor(t,n))}))}return e}function l(e,n){if(null==e)return{};var t,a,r=function(e,n){if(null==e)return{};var t,a,r={},o=Object.keys(e);for(a=0;a<o.length;a++)t=o[a],n.indexOf(t)>=0||(r[t]=e[t]);return r}(e,n);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);for(a=0;a<o.length;a++)t=o[a],n.indexOf(t)>=0||Object.prototype.propertyIsEnumerable.call(e,t)&&(r[t]=e[t])}return r}var i=a.createContext({}),c=function(e){var n=a.useContext(i),t=n;return e&&(t="function"==typeof e?e(n):s(s({},n),e)),t},d=function(e){var n=c(e.components);return a.createElement(i.Provider,{value:n},e.children)},u={inlineCode:"code",wrapper:function(e){var n=e.children;return a.createElement(a.Fragment,{},n)}},p=a.forwardRef((function(e,n){var t=e.components,r=e.mdxType,o=e.originalType,i=e.parentName,d=l(e,["components","mdxType","originalType","parentName"]),p=c(t),h=r,m=p["".concat(i,".").concat(h)]||p[h]||u[h]||o;return t?a.createElement(m,s(s({ref:n},d),{},{components:t})):a.createElement(m,s({ref:n},d))}));function h(e,n){var t=arguments,r=n&&n.mdxType;if("string"==typeof e||r){var o=t.length,s=new Array(o);s[0]=p;var l={};for(var i in n)hasOwnProperty.call(n,i)&&(l[i]=n[i]);l.originalType=e,l.mdxType="string"==typeof e?e:r,s[1]=l;for(var c=2;c<o;c++)s[c]=t[c];return a.createElement.apply(null,s)}return a.createElement.apply(null,t)}p.displayName="MDXCreateElement"},1793:(e,n,t)=>{t.r(n),t.d(n,{assets:()=>i,contentTitle:()=>s,default:()=>u,frontMatter:()=>o,metadata:()=>l,toc:()=>c});var a=t(7462),r=(t(7294),t(3905));
1const o={title:"Running a Sonar Server",id:"server",sidebar_position:1},s=void 0,l={unversionedId:"guides/server",id:"guides/server",title:"Running a Sonar Server",description:"To interact with Sonar through the Command line, the JavaScript client or the user interface, you need to run a Sonar server. The server joins the Sonar peer to peer network and exposes a HTTP API to interact with the content stored on this instance.",source:"@site/docs/guides/1_server.md",sourceDirName:"guides",slug:"/guides/server",permalink:"/docs/guides/server",draft:!1,editUrl:"https://github.com/facebook/docusaurus/tree/main/packages/create-docusaurus/templates/shared/docs/guides/1_server.md",tags:[],version:"current",sidebarPosition:1,frontMatter:{title:"Running a Sonar Server",id:"server",sidebar_position:1},sidebar:"tutorialSidebar",previous:{title:"Guides",permalink:"/docs/category/guides"},next:{title:"Client and Workspaces",permalink:"/docs/guides/api-client"}},i={},c=[{value:"Installation",id:"installation",level:2},{value:"Usage",id:"usage",level:2}],d={toc:c};
1function u(e){let{components:n,...t}=e;return(0,r.kt)("wrapper",(0,a.Z)({},d,t,{components:n,mdxType:"MDXLayout"}),(0,r.kt)("p",null,"To interact with Sonar through the Command line, the JavaScript client or the user interface, you need to run a Sonar server. The server joins the Sonar peer to peer network and exposes a HTTP API to interact with the content stored on this instance. "),(0,r.kt)("h2",{id:"installation"},"Installation"),(0,r.kt)("p",null,"You can easily install the sonar server globally through yarn or npm."),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-bash"},"yarn global add @arsonar/server\n")),(0,r.kt)("p",null,"Now run a Sonar server."),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-bash"},"sonar start\n")),(0,r.kt)("p",null,"The built-in CLI commands are documented:"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-bash"},"sonar help\n")),(0,r.kt)("p",null,"When including the Sonar server within a Node.js application, you can also start it programmatically."),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-javascript"},"const Server = require('@arsonar/server/server.js')\nconst opts = {\n  port: 9191,\n  hostname: 'localhost',\n  storage: './data',\n}\nconst server = new Server(opts)\nawait server.start()\n")),(0,r.kt)("h2",{id:"usage"},"Usage"),(0,r.kt)("p",null,"The Sonar server ships a (small) CLI that allows to interact with a running server through the command line. Currently, Sonar is mostly focused at being used programmatically through the JavaScript client. See the next section for an intro."),(0,r.kt)("p",null,"In the following section, we'll outline how Sonar can be used on the command line. As Sonar is a peer-to-peer tool, we'll assume that you have Sonar installed on two machines."),(0,r.kt)("p",null,"First, let's install and start Sonar on two machines. These can be any computer: A server, a laptop, a Raspberry Pi, etc."),(0,r.kt)("blockquote",null,(0,r.kt)("p",{parentName:"blockquote"},"In the code samples below, lines that are typed into the terminal start with a $. Lines without a $ at the beginning show the output of the commands. Lines starting with # are comments and should be ignored.")),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-sh"},"# on machine 1\n$ npm install -g @arsonar/server\n$ sonar start\n")),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-sh"},"# on machine 2\n$ npm install -g @arsonar/server\n$ sonart start\n")),(0,r.kt)("p",null,"Keep both commands running! For the rest of the tutorial, create a new terminal window on each machine."),(0,r.kt)("p",null,"Now, let's create a colletion on machine 1, and share it with machine 2."),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-sh"},"# on machine 1\n$ sonar collection create tutorial\nName:        tutorial\nPrimary key: 68c545f692d3c575b4a8d384ba896c3961e81c2c0d31c90bcec1757a15e9e7a5\nShared:      Yes\nLocal key:   68c545f692d3c575b4a8d384ba896c3961e81c2c0d31c90bcec1757a15e9e7a5\nLength:      2\nFeeds:\n        Key: 68c545f692d3c575b4a8d384ba896c3961e81c2c0d31c90bcec1757a15e9e7a5\n        Type: sonar.root Length: 2 Size: 157 B Writable: yes\n")),(0,r.kt)("p",null,'This created a new collection on machine1. Now, copy the "Primary key" shown in the command\'s output, and run the following command on machine2. Note that ',(0,r.kt)("inlineCode",{parentName:"p"},"tutorial")," here is just an informational name, you can chose whatever name you like and it does not have to be the same on the two machines."),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-sh"},"# on machine 2\n$ sonar collection create tutorial 68c545f692d3c575b4a8d384ba896c3961e81c2c0d31c90bcec1757a15e9e7a5\nName:        tutorial\nPrimary key: 68c545f692d3c575b4a8d384ba896c3961e81c2c0d31c90bcec1757a15e9e7a5\nShared:      Yes\nLocal key:   b9413ee100151fe0d4d9b524997151189fa7bef34c5938dd6b93dfc49ad7facd\nLength:      2\nFeeds:\n        Key: 68c545f692d3c575b4a8d384ba896c3961e81c2c0d31c90bcec1757a15e9e7a5\n        Type: sonar.root Length: 0 Size: 0 B Writable: no\n        Key: b9413ee100151fe0d4d9b524997151189fa7bef34c5938dd6b93dfc49ad7facd\n        Type: sonar.root Length: 2 Size: 157 B Writable: yes\n")),(0,r.kt)("p",null,"Let's add some data to the collection on machine1. Sonar includes a few data types and schemas. You can also add your own. The simplest schema is ",(0,r.kt)("inlineCode",{parentName:"p"},"sonar/entity"),", which is a record that only contains a label. We'll use this for the tutorial."),(0,r.kt)("p",null,"With the ",(0,r.kt)("inlineCode",{parentName:"p"},"sonar db"),"  commands, you can save and read records from the database in JSON format. Let's do this on machine 1."),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-sh"},'# on machine 1\n$ echo \'{"label":"hello world"}\' | sonar -c tutorial db put -t sonar/entity\ntbn3346zvnjxag4g6xygmbd75g\n')),(0,r.kt)("p",null,"This will print the record's ID."),(0,r.kt)("p",null,"The record should now automatically sync to machine 2 over Sonar's peer-to-peer network! When syncing, the full-text search index is synced as well. This means that after running the above command, you should be able to search for a term included in the record on machine 2."),(0,r.kt)("p",null,"Let's use the ",(0,r.kt)("inlineCode",{parentName:"p"},"search")," command:"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-sh"},"# on machine 1 or 2, doesn't matter because the index is synced now\n$ sonar -c tutorial search hello\n1 total\n0  Entity  tbn3346zvnjxag4g6xygmbd75g hello world\n")),(0,r.kt)("p",null,"Or the ",(0,r.kt)("inlineCode",{parentName:"p"},"db get")," command to get an invididual record:"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-sh"},'# on machine 1 or 2, doesn\'t matter because the index is synced now\n$ sonar -c tutorial db get tbn3346zvnjxag4g6xygmbd75g\nid       tbn3346zvnjxag4g6xygmbd75g\ntype     sonar/entity@0\nversion  68c54..39@3\nlabel    "foo bar"\n-------\n')),(0,r.kt)("p",null,"That's it for now! You can explore the built-in commands using ",(0,r.kt)("inlineCode",{parentName:"p"},"sonar help")," and ",(0,r.kt)("inlineCode",{parentName:"p"},"sonar help <COMMAND>")," for the individual commands."))}u.isMDXComponent=!0}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.