0

I have two different Laravel 5.4 apps, a restaurant menu system to recieve and manage orders, and one website from where customer can place their orders. Both apps run on different server(localy), which means, in my (windows)system I can run only one app at a time(localhost:8000). Both are using the same database tables. My question is how can I notify the restaurant menu system when user places an order from the website i.e., adding new row to Orders table in db? I need a notification as well as auto generate new row in the table like here: Restaurant Menu System . I have tried doing it with JQuery Ajax, but failed as there is nothing to trigger the ajax function in order page. Tried JQuery setInterval() from here but it seems like a very inefficient way and also gives an error of Uncaught SyntaxError: Invalid or unexpected token. I want to be as smooth as Facebook notifications. Is there any package or trick to do it?

The website looks just like any other ecommerce website with a cart and checkout system from where user can pay and place orders. Any leads is appreciated.

  • What have you tried, if the answer is nothing, try looking at laravel Echo, this is exactly the kind of live notification you want – milo526 Aug 11 '17 at 20:13
  • I think Laravel Echo works for only notification in the same app but different users, because any tutorials related to Echo shows notifications in the same app. – creatorsTake Aug 11 '17 at 20:19
  • How is there nothing to trigger the ajax? Don't they need to click on something to order it? Why can't you use that? Also you will do yourself a huge favor if you can figure out how to set up vhosts so you can have both of these apps running side by side (or use Laragon to handle it for you). – user1669496 Aug 11 '17 at 20:42
  • If you read my question properly, I wrote user places order from the website, and I need new order to be shown in the restaurant menu system which is a different app from website. Running both the app at the same time won't solve this problem, but thanks for the suggestion. – creatorsTake Aug 11 '17 at 20:53

1 Answers1

0

You have two options that I can think of.

One is a technique called comet which I believe Facebook uses or at least used at one point. It basically opens an ajax connection with your server and your server will occasionally check to see if there are any changes, in your case new orders, and when there is, will respond to the request appropriately. A very basic version of what that might look like is...

while (true) {
    $order = Order::where('new', 1)->first();

    if ($order !== null) {
        $order->new = 0;
        $order->save();
        return $order;
    }

    sleep(5);  // However long you want it to sleep for each time it checks
}

When you open up an ajax connection to this, it's just going to wait for the server to respond. When an order is made and the server finally does respond, your ajax function will get a response and you will need to do two things.

  1. Show the order and do whatever other processing you want to do on it
  2. Re-open the connection which will start the waiting process again

The disadvantage to this approach is it's still basically the setInterval approach except you've moved that logic to the server. It is more efficient this way because the biggest issue is it's just a single request instead of many so maybe not a big deal. The advantage is it's really easy.

The second way is a little bit more work I think but it would be even more efficient.

https://laravel.com/docs/5.4/broadcasting

You'd probably have to setup an event on your orders table so whenever anything is created there, it would use broadcasting to reach out to whatever javascript code you have setup to manage that.

user1669496
  • 32,176
  • 9
  • 73
  • 65