Skip to content

Latest commit

 

History

History
121 lines (97 loc) · 2.7 KB

channels.md

File metadata and controls

121 lines (97 loc) · 2.7 KB

Channels

Listen on Syncano Realtime Channels.

Methods

Name Description
s.listen Listen on given channel

s.listen(endpoint, params?)

Subscribe to given Syncano endpoint. s.listen returns instance of WebSocket.

// your-client-side-file.js
// chat - socket name
// poll-messages - endpoint name
s.listen('chat/poll-messages')
  .addEventListener('message', message => {
    // Handle message
  })

Public channels

# chat/socket.yml
endpoints:
  global-messages:
    channel: global-messages
  create-message:
    file: src/create-message.js
// chat/src/create-message.js
import {data, channel} from '@syncano/core'

data.messages
  .create({
    content: ARGS.content,
    user: META.user.id
  })
  .then(message => {
    channel.publish(`global-messages`, message)
  })

Room channels

# chat/socket.yml
endpoints:
  private-messages:
    channel: private-messages.{room}
  create-message:
    file: src/create-message.js
// chat/src/create-message.js
import {data, channel} from '@syncano/core'

data.messages
  .create({
    room_id: ARGS.room_id,
    content: ARGS.content,
    user: META.user.id
  })
  .then(message => {
    channel.publish(`private-messages.${ARGS.room_id}`, message)
  })
s.listen('chat/private-messages', {room: 1})
  .addEventListener('message', message => {
    // Handle message
  })

User channels

First, you have to use special variable {user} in your channel name. It'll be used to check if user trying to access endpoint have rights to do it.

# notifications/socket.yml
endpoints:
  get:
    channel: notifications.{user}
  notify:
    file: src/notify.js

Then you can subscribe to channel by passing socket and endpoint name. User channels require user_key to be send in payload.

s.listen('notifications/get', {user_key: 'USER_KEY'})
  .addEventListener('message', notification => {
    // Handle notification
  })

To publish to the user channel, you have to know it's username.

In channel name notifications.{user} - {user} must always be username, not id or any other property.

// notifications/src/notify.js
import {data, channel} from '@syncano/core'

data.notifications
  .create({
    content: ARGS.content,
    username: ARGS.username
  })
  .then(notification => {
    channel.publish(`notifications.${ARGS.username}`, notification)
  })