How do you store data in os-js?


#1

Sorry for all of the questions,

is there a native way to store and retrieve persistent data from a database in os-js?

For example, if each user had a ‘funny name’ and you wanted to store that funny name for each user, and make it appear in the panel-item, how would you go about doing that?

also is there a way to get a panel item to communicate with a native app?

For example, how do I figure out whether an application instance is running or not from the panel(or any other piece of code)? And how can I send arguments to that application after its running?

Thankyou very much for the help!


#2

Since I had to restore a snapshot of this server, my original comment was lost – so I’ll try to recreate it.


You can store data with the Settings provider. By default this is set up to use localStorage, but you can use this to make it persist in a database:

You can communicate between anything using the internal bus-es (EventEmitter-s).

You currently can only get a list of running application names with core.make('osjs/packages').running().

If you want to get a list of running instances, I can probably add that.

At the moment the easiest way to do this is communicate via the core:

// Your application
const handleExternalMessage = (...args) => {};
core.on('tristan/application', handleExternalMessage);
proc.on('destroy', () => core.off('tristan/application', handleExternalMessage))

// Anywhere else
core.emit('tristan/application', 1, 2, 3);

#3

I created an issue relating to this


#4

This has been implemented in the newly published @osjs/client package :slight_smile:

Edit Updated manual:

https://manual.os-js.org/v3/tutorial/application/#events


#5

Perfect! That looks great! Very useful!


#6

You can also store in the user home directory actually :slight_smile:

// Server (src/server/index.js)
osjs.register(SettingsServiceProvider, {
  args: {
    adapter: 'fs'
  }
});

// Client (src/client/index.js)
osjs.register(SettingsServiceProvider, {
  args: {
    adapter: 'server'
  }
});

Just make sure to have the latest updates :slight_smile: