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

**URL:** https://community.dreamfactory.com/t/chat-e-g-using-faye-rabbitmq-cache-via-redis/1095
**Category:** Connecting to NoSQL
**Created:** [May 12, 2015, 3:42pm UTC](https://community.dreamfactory.com/t/chat-e-g-using-faye-rabbitmq-cache-via-redis/1095 "2015-05-12T15:42:36Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Rai](https://avatars.discourse-cdn.com/v4/letter/r/67e7ee/32.png) [@Rai](https://community.dreamfactory.com/u/Rai)
#### Post date: [May 12, 2015, 3:42pm UTC](https://community.dreamfactory.com/t/chat-e-g-using-faye-rabbitmq-cache-via-redis/1095/1 "2015-05-12T15:42:36Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![jeffreystables](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/jeffreystables/32/173_2.png) [@jeffreystables](https://community.dreamfactory.com/u/jeffreystables)
#### Post date: [May 12, 2015, 7:51pm UTC](https://community.dreamfactory.com/t/chat-e-g-using-faye-rabbitmq-cache-via-redis/1095/2 "2015-05-12T19:51:09Z")

</div>

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](https://www.dreamfactory.com/support/). Of course the forum, and this post, is the best way to get advice and input from the community at large.

---

<div class="post-metadata">

### Author: ![Rai](https://avatars.discourse-cdn.com/v4/letter/r/67e7ee/32.png) [@Rai](https://community.dreamfactory.com/u/Rai)
#### Post date: [May 12, 2015, 9:22pm UTC](https://community.dreamfactory.com/t/chat-e-g-using-faye-rabbitmq-cache-via-redis/1095/3 "2015-05-12T21:22:06Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![jeffreystables](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/jeffreystables/32/173_2.png) [@jeffreystables](https://community.dreamfactory.com/u/jeffreystables)
#### Post date: [May 19, 2015, 3:32pm UTC](https://community.dreamfactory.com/t/chat-e-g-using-faye-rabbitmq-cache-via-redis/1095/4 "2015-05-19T15:32:47Z")

</div>

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

> **[dreamfactorysoftware/app-faye-chat](https://github.com/dreamfactorysoftware/app-faye-chat)**
>
> Skype like chat application using faye and angularJS - dreamfactorysoftware/app-faye-chat

---

<div class="post-metadata">

### Author: ![philicious](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/philicious/32/689_2.png) [@philicious](https://community.dreamfactory.com/u/philicious)
#### Post date: [April 28, 2016, 4:46pm UTC](https://community.dreamfactory.com/t/chat-e-g-using-faye-rabbitmq-cache-via-redis/1095/5 "2016-04-28T16:46:33Z")

</div>

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](https://github.com/dreamfactorysoftware/dreamfactory/blob/master/config/app.php#L190)
