03.12.2015 Views

laravel4cookbook

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Real Time Chat 190<br />

.<br />

If you want to pre-populate your chat application with a history; you could feed this fixture<br />

configuration with data from your server.<br />

Showing Chat Messages<br />

Now the index template will show only the heading and form elements, but no chat messages. In<br />

order to populate these; we need to store a reference to the application data store:<br />

1 var store;<br />

2<br />

3 App.IndexRoute = Ember.Route.extend({<br />

4 "init" : function() {<br />

5 store = this.store;<br />

6 },<br />

7 "model" : function () {<br />

8 return store.find("message");<br />

9 }<br />

10 });<br />

.<br />

This was extracted from public/js/shared.js.<br />

We store this reference because we will need to push rows into the store once we receive them from<br />

the open socket. This leads us to the changes to web sockets:<br />

1 try {<br />

2 var id = 1;<br />

3<br />

4 if (!WebSocket) {<br />

5 console.log("no websocket support");<br />

6 } else {<br />

7<br />

8 var socket = new WebSocket("ws://127.0.0.1:7778/");<br />

9 var id = 1;<br />

10<br />

11 socket.addEventListener("open", function (e) {<br />

12 // console.log("open: ", e);

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!