How to use Emitter to emit events from backend to frontend

I would like to send notifications from backend to frontend. How do i use the Emitter class from @theia/core to emit events/notifications from backend to frontend.
Can i have any sample implementation?

[original thread by asvinwin123]

No, you will need to provide a JSON-RPC client to notify it by a server.

on the frontend though you can implement such client using events

look at FileSystemWatcherClient for example: https://github.com/theia-ide/theia/blob/0d53466b8db1335d1baca2aba2504e556236e3ad/packages/filesystem/src/common/filesystem-watcher-protocol.ts#L38

[asvinwin123]

Thank you. will check that.

[asvinwin123]

@anton-kosyakov I have tried with filesystem watcher using JSON RPC. It seems to be complicated. Could not understand the flow mechanism how it is being used in frontend and backend.

Could you elaborate which part is not clear?

  • define server and client interrfaces
  • implement server on backend, in order to notify a client call a corresponing method
  • implement client on frontend which converts calls into events
  • bind server implementation in the backend module, and client implementation in the frontend module

I don’t have time right now to prepare an example. If you do a sample extension, i can review it and help fixing it.

[asvinwin123]

@anton-kosyakov Thank you very much. Shall update you with my code implementation soon.

[asvinwin123]

@anton-kosyakov Hi here is the sample code implementation,

I have used JsonRPC to communicate from frontend to backend by passing the client interface to backend method. I am able to receive the client object from backend, however in the backend using the client object could not access the client method.

We have mentioned few comments to make you understand the implementation we are trying to achieve. Let me know if you need any clarifications. Thank you.

setClient is never called on the server on the backend, it should be:

bind(ConnectionHandler).toDynamicValue(ctx =>
        new JsonRpcConnectionHandler<TestClient>(testPath, client => {
            const server = ctx.container.get<TestServer>(TestServer);
            server.setClient(client);
            return server;
        })
    ).inSingletonScope();

also if you want to have a new server per a connection then you should not put in singleton scope, but in transient

and dispose a server when a client is gone

i.e. client.onDidCloseConnection(() => server.dispose());

[asvinwin123]

@anton-kosyakov Thank you very much. It worked.