The dbus approach for single client/server snap

Hello,

I’m trying to use DBus example[1] to pack a snap with client/server apps over DBus API, and of course I’m still learning DBus interface to be applied. From our doc[2] mentioned that it provides system DBus and session DBus available, and my first problem here (sorry if my head is not clear) is, based on this example, I think it should be able to use session DBus via plugs/slots even if this snap is not for Desktop. I tried to use session as server slot first, and make sure that client’s plug is connected to server’s slot:

slots:
  dbus-server-slot:
    interface: dbus
    bus: session
    name: in.softprayog.add_server
  dbus-client-slot:
    interface: dbus                                                                                                                                                                                        
    bus: session
    name: in.softprayog.add_client

plugs:
  dbus-server-plug:
    interface: dbus
    bus: session
    name: in.softprayog.add_server

apps:
  server:
    command: bin/server
    slots: [dbus-server-slot]

  client:
    command: bin/client
    slots: [dbus-client-slot]
    plugs: [dbus-server-plug]

after server part is run, the client will report the following

$ dbus-test-snap.client 
Please type two numbers: 3 3
The name in.softprayog.add_server was not provided by any .service files

I’m not sure if it means that I have to put in.softprayog.add_server.service under prime/usr/share/dbus-1/services/, but even I did this it seems not to be helpful because this service was not there! (I’m using snap run --shell to check this) Anyway using session DBus has some troubles for me.

Then, I switch to system DBus in the C code and snapcraft.yaml to have changes:

Consequently, it works with root. So another question will be: Is there a better way or an example to demonstrate how a single snap / two snaps properly works with session or system DBus? It could be important for some developers tried to deliver such snap based on original application, especially when the customer is asking.

Thanks,
Woodrow

[1] https://www.softprayog.in/programming/d-bus-tutorial
[2] https://snapcraft.io/docs/dbus-interface

You need to use the experimental user sessions along with the experimental DBus activation features. If you want your service to be on the user session bus (not system bus) and D-Bus activatable use something like this:

plugs:

slots:
  dbus-server-slot:
    interface: dbus
    bus: session
    name: in.softprayog.add_server

plugs:
  dbus-client-plug:
    interface: dbus
    bus: session
    name: in.softprayog.add_server


apps:
  server:
    command: bin/server
    slots:
      - dbus-server-slot
    passthrough:
      daemon-scope: user
      activates-on: 
        - dbus-server-slot
  client:
    command: bin/client 
    plugs: 
      - dbus-client-plug

and then turn on the experimental settings in snapd:

snap set system experimental.dbus-activation=true
snap set system experimental.user-daemons=true

and install the snap. At this point things should work as you expect running the client as a normal user will activate and start the user session service of server. You do not need to prime or stage any system service units, snapd generates those for you from the snap.yaml

1 Like

@ijohnson Thank you! Very useful and we can have a good shape now.