0.2.9 • Published 6 years ago

syncano-client v0.2.9

Weekly downloads
4
License
MIT
Repository
github
Last release
6 years ago

CircleCI codecov

Syncano Client Library

This library enables you to interact with the Syncano Sockets via Javascript.

Getting started

Installing from NPM

npm install syncano-client --save

Also available at UNPKG

<script src="https://unpkg.com/syncano-client"></script>

Usage

The library supports the CommonJS syntax:

var Syncano = require('syncano-client')

You can also use it with ES6 modules:

import Syncano from 'syncano-client'

Creating a connection

To create a connection, simply initialize the Syncano object with instance name:

const s = new Syncano('MY_INSTANCE_NAME')

Constructor

Syncano(instanceName, options?)

ParameterTypeDescription
instanceNameStringSyncano instance name. You can create one using Syncano CLI.
optionsObjectOptional connection config.
options.hostStringSyncano host name.
options.tokenStringAllows you to initialize authorized connection.
options.loginMethodFunctionDefine custom login method

Methods

s(endpoint, data?, options?)

Alias of s.post method.

s.login(username, password)

Before you can send authorized requests, you need to login user with username and password. This method will automatically save user token for future requests.

s.login('john.doe', 'secret')
  .then(user => console.log(`Hello ${user.first_name}`))
  .catch(() => console.log('Invalid username or password.'))

s.logout()

Remove user token for future requests.

s.setToken(token)

Used to restore client session with token.

ParameterTypeDescription
tokenStringUser token used to authorize requests.

To remove token, call setToken without parameter:

s.setToken()

s.get(endpoint, data?, options?)

Send GET request to Syncano socket.

ParameterTypeDescription
endpointStringName of socket and endpoint joined with '/':
dataObjectOptional object send with request.
optionsObjectOptional request configuration.
// countries - socket name
// list - endpoint name
s.get('countries/list')

// Pass additional data to request
s.get('countries/list', { order_by: 'name' })

// Configure request
s.get('countries/list', {}, {
  headers: {
    'Content-Type': 'application/json'
  }
})

For more options, view axios documentation

s.post(endpoint, data?, options?)

Send POST request to Syncano Socket. View s.get method for more info.

s.delete(endpoint, data?, options?)

Send DELETE request to Syncano Socket. View s.get method for more info.

s.put(endpoint, data?, options?)

Send PUT request to Syncano Socket. View s.get method for more info.

s.patch(endpoint, data?, options?)

Send PATCH request to Syncano Socket. View s.get method for more info.

s.subscribe(endpoint, data?, callback)

Subscribe to given Syncano endpoint. Callback is fired each time something is pushed to channel bound to endpoint.

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

Public channels

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

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: scripts/create-message.js
// chat/scripts/create-message.js
import {data, channel} from 'syncano-server'

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.subscribe('chat/private-messages', {room: 1}, 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: scripts/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.subscribe('notifications/get', {user_key: 'USER_KEY'}, 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/scripts/notify.js
import {data, channel} from 'syncano-server'

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

s.subscribe.once(endpoint, data?, callback)

Sometimes you want to listen only for one event and after that stop handling new events.

s.subscribe.('user-auth/verify', isVerified => {
  // Handle verification
})
0.16.1-1

6 years ago

0.16.1-0

6 years ago

0.2.6-3

7 years ago

0.2.9

7 years ago

0.2.8

7 years ago

0.2.6-2

7 years ago

0.2.7

7 years ago

0.2.6-1

7 years ago

0.2.6

7 years ago

0.2.6-0

7 years ago

0.2.5

7 years ago

0.2.1-9

7 years ago

0.2.1-8

7 years ago

0.2.4

7 years ago

0.2.1-7

7 years ago

0.2.3

7 years ago

0.2.1-6

7 years ago

0.2.1-5

7 years ago

0.2.1-4

7 years ago

0.2.1-3

7 years ago

0.2.1-2

7 years ago

0.2.1-1

7 years ago

0.2.1

7 years ago

0.2.1-0

7 years ago

0.15.2-8

7 years ago

0.15.2-7

7 years ago

0.15.2-6

7 years ago

0.15.2-5

7 years ago

0.15.2-4

7 years ago

0.15.2-3

7 years ago

0.15.2-2

7 years ago

0.15.2-1

7 years ago

0.15.2-0

7 years ago

0.13.2-0

7 years ago

0.15.1

7 years ago

0.15.0

7 years ago

0.14.0

7 years ago

0.13.1

7 years ago

0.13.0

7 years ago

0.12.0

7 years ago

0.11.2

7 years ago

0.11.1

7 years ago

0.11.0

7 years ago

0.10.0

7 years ago

0.9.0

7 years ago

0.8.0

7 years ago

0.7.0

7 years ago

0.6.0

7 years ago

0.5.0

7 years ago

0.4.0

7 years ago

0.3.0

7 years ago

0.2.0

7 years ago

0.1.0

7 years ago