socketsupply / socket-examples

Socket Runtime Examples – see how your favorite front-end framework is used to build cross platform UIs
https://socketsupply.co/guides
MIT License
117 stars 12 forks source link

Question: how to use from portable nodejs #71

Closed tiptronic closed 1 year ago

tiptronic commented 1 year ago

I just found this project and I really like your approach!!

However, I couldn't figure out how to use it in my existing node-project.

So I hope it's okay to ask a question here:

I tried installing the ssc-node or socket-node, but couldn't find any docs how to use it.

Here's what I'd like to do:

Any pointer on how to make that work from my index.js would be greatly appreciated. (Also any pointers to examples or documentation).

TIA Andy

jwerle commented 1 year ago

I just found this project and I really like your approach!!

However, I couldn't figure out how to use it in my existing node-project.

So I hope it's okay to ask a question here:

I tried installing the ssc-node or socket-node, but couldn't find any docs how to use it.

Here's what I'd like to do:

  • from my index.js I'd like to open a window and talk to it (either through sockets, or WebSockets)
  • I have no $PATH variable available, because the app runs on a portable nodejs on a user's machine (so the app just should open an empty window and then gets all it needs from the socket)

Any pointer on how to make that work from my index.js would be greatly appreciated. (Also any pointers to examples or documentation).

TIA Andy

hi @tiptronic! thanks for dropping by! do you have any code examples you can share with us to give us a better idea what you are working with?

tiptronic commented 1 year ago

hi @jwerle,

thanks for your reply and - haha - I was hoping for some code-examples from you :) It could very well be, I misunderstood the socket-node usage alltogether, but I thought it allows creating an ipc style communication from an index.js to the socketsupply window.

Anyway - let me try to explain again:

If I remove import socket from '@socketsupply/socket-node' the error goes away...

What I would like to know, HOW can I communicate with the window using socket-node here? (Unfortunately I haven't found any docs)... or did I misunderstand the usage of socket-node (and should just spawn a child-process and calling the socketsupply binary directly)?

Here's the error:

at addChunk (node:internal/streams/readable:332:12) {
  errors: [
    {
      detail: undefined,
      id: '',
      location: {
        column: 23,
        file: 'node_modules/@socketsupply/socket-node/index.js',
        length: 11,
        line: 2,
        lineText: "import { format } from 'node:util'",
        namespace: '',
        suggestion: ''
      },
      notes: [
        {
          location: null,
          text: `The package "node:util" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.`
        }
      ],
      pluginName: '',
      text: 'Could not resolve "node:util"'
    },
    {
      detail: undefined,
      id: '',
      location: {
        column: 29,
        file: 'node_modules/@socketsupply/socket-node/index.js',
        length: 13,
        line: 3,
        lineText: "import { EventEmitter } from 'node:events'",
        namespace: '',
        suggestion: ''
      },
      notes: [
        {
          location: null,
          text: `The package "node:events" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.`
        }
      ],
      pluginName: '',
      text: 'Could not resolve "node:events"'
    }
  ],
  warnings: []
}

Finally:

TIA andy

chicoxyzzy commented 1 year ago

Hi @tiptronic

From the main window you can open backend with application.backend.open(). This will run a command which is specified in the socket.ini configuration file. After that you should be able to send or receive messages. On the Node.js side there are the same send and on methods

socket:* modules are Socket Runtime built-in modules, they are not available in the Node.js code.

You can find socket-node documentation here. We will add it to the docs website soon.

If you want to bundle Node.js into your desktop application, you can take a look at node-backend-bundled example.

tiptronic commented 1 year ago

Hi @chicoxyzzy

thx for the info, but for me it's the other way round:

my process is already running (node index.js) and now I want to open the window (like an external window) and talk to it ;)

From what I read in the socket-node code, it looks like I can't use socket and `socket-node' like this, but spawn a child-process?

If I understand the node-backend-bundled example correctly, I would run another node-process (as backend), right?

Or did I mis-understand something here?

chicoxyzzy commented 1 year ago

The backend process in Socket Runtime is optional and only available for desktop applications. Socket provides built-in socket:* modules which are mostly compatible with Node's node:* modules. You can either drop the Node.js dependancy if you have all the necessary stuff in the Socket JS API and use socket:* modules or you need start the backend process from the render process. It is different from how Electron works. Socket's main process is the Runtime itself. It opens a webview where you can start the bundled backend process (and show error if something goes wrong) or propose to setup / download the backend. If you want to start a desktop Socket application from the Node.js, you probably need the child_process and spawn / exec and wire IPC via stdin / stdout if you need communication between Node.js that starts the application and Socket app. It is highly recommended that backend is only used for heavy computations or for the things, that aren't available in Socket Runtime (yet).

lamiazar commented 1 year ago

@tiptronic I hope you got your question answered, if you have any additional questions please feel free to ask us on our Discord channel or open an issue 🙏