03.12.2015 Views

laravel4cookbook

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Real Time Chat 187<br />

123<br />

124 protected function getOptions()<br />

125 {<br />

126 return [<br />

127 [<br />

128 "port",<br />

129 null,<br />

130 InputOption::VALUE_REQUIRED,<br />

131 "Port to listen on.",<br />

132 null<br />

133 ]<br />

134 ];<br />

135 }<br />

136 }<br />

This file should be saved as workbench/formativ/chat/src/Formativ/Chat/Command/Serve.php.<br />

.<br />

The reason for us adding the event emitter to the equation should now be obvious — we need a way<br />

to tie into the delegated events, of the Chat class, without leaking the abstraction we gain from it.<br />

In other words; we don’t want the Chat class to know of the existence of the artisan command.<br />

Similarly; we don’t want the artisan command to know of the onOpen, onMessage, onError and<br />

onMessage methods so instead we use a publish/subscribe model for notifying the command of<br />

changes. The result is a clean abstraction.<br />

The fire() method gets (or defaults) the port and starts the Ratchet web socket server.<br />

.<br />

The method we’re using, to start the server, is not the only way it can be started. You can learn<br />

more about the web socket server at: http://socketo.me/docs/websocket.<br />

Connecting To The Socket Server<br />

To connect to the web socket server; we need to add a bit of vanilla JavaScript:

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

Saved successfully!

Ooh no, something went wrong!