I am building in app in which each app instance has many clients, each of them with a phone number. The app instance user would like to communicate with their clients. When they send a message to their client it is routed to said client as an SMS message; when the client replies to the SMS it is routed to the app instance as a message on the messaging function of the app.
I have a bit of a problem visualizing the architecture of receiving replies from the phone of the client in the messaging server and sending it to the app instance. I tried two ways:
- Publish messages to a port on the messaging server, and have the app instances listen on it: the problem is that every app instance can now listen to every message, which is of course extremely unsecure. I could encrypt the messages and ensure only the correct app instance can decrypt the messages intended to it, but just having the message asset available to every app instance seems like an anti-pattern. I can avoid it by spinning up an instance of the server for every instance of the app but I would like to avoid making more money for Jeff Bezos.
- Get the app instance address and send directly to a port on it, and have the instance listen on that port: keeping track of client (here the app instance is a client in the meaning of server-client relationship) address is a form of keeping client state, which is a REST violation and also super difficult. I'd have to start dealing with network proxies or users of the app changing devices.
The other option is to publish the replies to a database and have the app instance continuously poll the database to update its messages/state. I would really like to avoid that as it's inefficient and not in real-time. This seems like it should be a solved problem as every messaging service that uses a server should have faced a similar problem.
TLDR; I would like a way for the server to publish messages to app instances but can't figure out how to do so securely and correctly.