Chat (e.g. using Faye/RabbitMQ) Cache via Redis?

We are trying to build a Chat app with a Dreamfactory DSP.

We’re betting on Dreamfactory as being a smarter alternative to building our own Rest API from scratch for the umpteenth time, and I have not hesitated in signing up for a paid support plan.

The Project
We are experimenting with frameworks/libraries like Faye to handle the realtime API for the chat service itself.

The realtime API will be mirrored by a REST API for things like chat history / user data. For example, if user ‘john’ writes a message in chatroom ‘lobby’ then we want to emit that message to all other subscribers to the chatroom but also create a new ‘message’ item in dreamfactory, so if a new subscriber joins ‘lobby’ then he can just GET ‘room/lobby’ (which will contain meta about the chatroom and the related messages) and start listening from there.

(is this a plausible/sensible approach or pattern?)

Problems
Our main hurdles are related to:

  1. Pub/Sub Access Control / Permissions / Security
  2. Socket/Session caching

We are unsure of how to best implement a Chat service that queries a Dreamfactory DSP to control publish/subscribe permissions. Has anyone had any experience in this area or can advise?

Why (Redis)?

  1. We are worried that SQL/NoSQL ‘Databases’ are too slow for highly transactional chat logic. Example: user ‘Mila’ has been invited to chatroom ‘Hollywood’ but user ‘Floyd’ has not been invited. ‘Floyd’ tries to listen in on the channel ‘Hollywood’. The Faye server checks the DSP model for the right permissions before allowing Floyd to listen.
  2. We want to support multiple simultaneous socket connections at scale so need(?) to implement a session cache using something like Redis?** We have discovered that Dreamfactory has (undocumented) Redis support. Would that be best used for chat session cache?

I would massively appreciate a conversation with someone with related experience who could advise on architecting and/or implementing the application.

Hi @Rai, glad to have you in the DreamFactory community and participating in the forum. If you’d like to take advantage of your DreamFactory subscription to get technical support, please contact us directly. Of course the forum, and this post, is the best way to get advice and input from the community at large.

Thanks @jeffreystables, I have emailed support directly as per your advice. I’ll leave this thread open just in case someone else has or had a similar use case in the past and would also benefit from the advice furnished.

Since the community may benefit, here is a link to the example chat app we developed using faye, AngularJS, and the DSP.

I had a similar problem/scenario:
Caching the session a custom service needs to auth/talk to a remote service so
a) custom service doesnt need to call login on the remote service all the time
b) session is persisted and allows parallel calls to the custom service without race-condition on the login

So what I came up with works like a charm and is quite simple to implement: Using DFs Redis backend
Ofc you need to to configure DF to use Redis as a cache first.

Then in your custom script do

    $redis = Redis::connection();
    $sessionExists = $redis->exists("magento_session");
    if($sessionExists) {
        $session = $redis->get("magento_session");
    } else {
        // call magento/login to get session
        $data = [
            'username' => {magento_api_user},
            'apiKey' => {magento_api_key}
        ];
        $res = $platform['api']->post->__invoke("magento/login", $data);
        $session = $res['content']['result'];
        $redis->set("magento_session", $session);
        $redis->expire("magento_session", 3600);
    }

The trick is that Redis is an available alias, see https://github.com/dreamfactorysoftware/dreamfactory/blob/master/config/app.php#L190

1 Like