Socket.IO

With *socketIO directive you can interact with Socket.IO in templates.

Creating connection, Listening and Emitting events

An example in COMPONENT.template.html.

<!-- Creating a connection -->
<ng-container *socketIO="let socket of 'http://...'">
  <!-- Socket {{ socket }} is a server interaction socket! -->

  <!-- You can listen events like that -->
  <ng-container *socketIoOn="let event on 'event name' from socket">
    <!-- All code here is pure, and is called only on event! -->
    {{ store.dispath({ type: 'new event': payload: event }) }}
  </ng-container>
  
  <!-- Also you can send messages to the socket -->
  <button (click)="socket.emit('click')">Click me!</button>
  <!-- And emit with any events -->
  <div (mousemove)="socket.emit('mousemove', $event.clientX, $event.clientY)"></div>
  <input (keypress)="socket.emit('type', $event.target.value)">
</ng-container>

Last updated