0

I have an index page, when a user logs in, it shows all tables (button of tables) in the database.Table can have an order. So, it works like when a user clicks a table, it redirects to the create path of order and create an order. Or if a table has an order, it redirects to order show page. Each order has a table_id. And table shows whether it is busy now or not by showing different color.

What I want to do is that changing table button's color real time. So that if one user creates order, the other user can see it immediately as table changes color. How can I achieve this? I am researching gen server and channel but I still am not familiar with those concepts.. any suggestions for this?

Thanks in advance

Justin Wood
  • 9,941
  • 2
  • 33
  • 46
D.R
  • 829
  • 4
  • 16
  • 30
  • You are definitely on the right track. Basically you want to have them both connect to the same channel, and when a user makes a change in the backend, have it publish a message to all subscribed clients, who will then update accordingly – Harrison Lucas Dec 04 '16 at 06:21
  • @HarrisonLucas Thank you for your comment. Now Im rendering tables on tableshow page, which is under page controller. I am thinking that make channel for `tableshow:*` and for * I want it to be user_id... is it possible to do that?? if not, would u mind elaborating a bit for designing the tableshow channel? thanks – D.R Dec 04 '16 at 06:57

1 Answers1

0

There are two ways to achieve realtime type functionality

  1. You can use Ajax to continuously poll your server asking for a new copy of that page example found here https://techoctave.com/c7/posts/60-simple-long-polling-example-with-javascript-and-jquery

  2. You can use a service like Pusher and Sockets which is the preferred way to achieve realtime. https://pusher.com/tutorials check out some of the chat tutorials to get a better understanding.

prola
  • 2,793
  • 1
  • 16
  • 15
  • 1
    While this answer is mostly correct, the phoenix framework comes with a simple way to deal with sockets. They call them channels. Unless something about channels are limiting you, you probably want to stick with those. – Justin Wood Dec 04 '16 at 12:56
  • I personally use laravel echo which also uses channels, client side code can listen to and pusher in my app but I agree the majority of frameworks have their own ways at implementing realtime. – prola Dec 05 '16 at 02:19