I am building an app using AngularJS and Laravel. For a database change to take instant effect in the view, I am running a fetchResults function every second. Works, but feels wrong. What are the other ways to achieve this?
Polling the server at regular intervals will be very inefficient. You could use Server Sent Events or Web Socket to achieve this efficiently. These methods sends updates only when changes happen on the server.
Learn More about SSE developer.mozilla.org/en-US/docs/Web/API/Server-s…. Not all browsers supports this though. or You could try sockets https://github.com/socketio/socket.io
As others have pointed out, you should use web sockets for that kind of requirement. I know it won't help you right now, but more streamlined event broadcasting (it's called Laravel Echo) is coming in Laravel 5.3.
Would second looking into web sockets - definitely a cleaner method to do what you need. An option here called Ratchet: http://socketo.me/
Sandeep Panda
co-founder, Hashnode
Continuous Polling can be inefficient. I suggest you use websockets for this. When the user comes your website open up a socket connection with your Socket Server. When something changes in DB your Socket Server will notify all the connected clients about the change and clients can then react to the changes.
See : Socket.io