Using Ember with Sockets

WebSockets make it possible to open a communication session with a server. Ember.js does not handle this natively. However, there are some easy-to-use add-ons that can be used to make this possible.

How to do it...

  1. In a new application, generate a new route and install the WebSockets add-on:
    $ ember install ember-websockets
    $ ember g component w-s
    $ ember g route ws
    

    This will install the component route and add-on that we need to begin.

  2. In the components template folder, edit the w-s.hbs file:
    // app/templates/components/w-s.hbs
    Welcome Chat!<br><br>
    
    Received Message: {{message}}<br>
    <button id="sendButtonPressed" {{action "sendButtonPressed"}}>Press Me</button>

    In this template, we have a message property and button that sends ...

Get Ember.js Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.