Skip to content

Adding websocket functionality

Melvin Mathew edited this page Jul 27, 2017 · 1 revision

WebSocket Implementation

Websockets have been implemented using the channels with with Redis backend. When a page is loaded we create a socket connection between the client and the server, by subscribing to a group. The group is named after the user's unique ID (Remember the group name should only contain alphanumerics, hyphen and period hence use websocket_group_name function before assigning the unique ID as the group name).

Add websocket functionality to any model

This is a 3 step process:

  1. Create a sqlalchemy ORM Mapper event. Mapper events provide hooks into critical sections of the mapper, including those related to object instrumentation, object loading, and object persistence. Here is an example of a hook for OwnerNotification models's after_create method.

  2. Within these hook, add a call to send_message function. This function has 3 arguments:

    1. group_name: the unique ID (email ID) of the user
    2. type: type of communication/message. e.g. type=notification for sending notification over websockets This will be used to identify the message on the client side
    3. message: message in JSON format
  3. Add code (here) to handle this new type of message on client side.

Clone this wiki locally