3

I am creating an application using hexagonal architecture. The application uses WebSocket connections to communicate with some outside clients. Communication works in both ways. the client can send a WebSocket message which the application needs to handle and the application can also send a WebSocket message to the client.

According to hexagonal architecture, an application service implements a port used by the driving adapter. Whereas, an application service uses the port implemented by a driven adapter. This will help me easily replace an old implementation with a new one by providing a new implementation for port (on the driving side) or adapter (on the driven side).

In my case of WebSocket communication, the WebSocket port acts both as the driving port (to handle messages coming from the actor) and the driven port (to send messages to the actor).

I am quite confused about how to solve this problem while sticking to the hexagonal architecture.

3
  • Can you define what you mean by "driving interface" and "driven interface"? Sep 13, 2022 at 15:42
  • Alistair Cockburn mentions the terms "driving adapter" and "driven adapter" in his original article on Hexagonal Architecture. They are also known as primary and secondary adapters. The primary adapter is something that drives something in the application (UI, Rest API, etc.) whereas the secondary adapter is something that an application uses (Database, Rest API to perform some action, etc.). I think I made a mistake by mentioning them as "interfaces". Sep 13, 2022 at 16:10
  • @Muhammad interfaces, abstract classes, API's, whatever. So long as they enable polymorphism it works. Sep 13, 2022 at 16:37

1 Answer 1

4

There is no problem. It is okay that the web socket service is both driving and driven.

Let's see an example.

  • You have a web shop where backend and frontend communicate via web socket.
  • When the user clicks on the buy button in the frontend, the backend needs to execute the according business logic.
  • When the backend detects that there are new items available, the frontend shoud display some message.

Using hexagonal architecture, the backend needs a BuyItemPort and a PushMessagePort.

Buying an item is business logic. Therefore, the BuyItemPort is implemented in the application layer. When the user clicks on the buy button, the frontend sends a message via web socket. It arrives on the web socket service in the backend, which calls the BuyItemPort. Therefore, the web socket service must be a driving adapter.

It is business logic that a push message should be sent when there are new items in the store. So there is some kind of NewItem event with an event handler in the application layer. The handler now calls the PushMessagePort, which sends the message via web socket to the frontend. Therefore, the web socket service must be a driven adapter.

1
  • This is summed up in Alistair Cockburne's article: "The distinction between ‘’primary‘’ (driving) and ’’secondary‘’ (driven) lies in who triggers or is in charge of the conversation." (emphasis and clarifications, mine). Sep 13, 2022 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.