Skip to content

client server

Emiel Mols edited this page Aug 12, 2015 · 1 revision

This module exposes functions to interact with the server-side part of your plugin.

Functions

call(rpcName, args...)

Call server-side code that is defined in server.js as exposed.client_$rpcName. When the client is not connected, the attempt to call the server is not always retried. When javascript functions are given as arguments, server-side will receive references to them as Callback objects. From there, they can be immediately replied to, or subscribed to a channel.

While the RPC call is in progress, the user interface for your plugin is blocked by an overlay, to indicate we're waiting for something and to prevent additional taps.

Example:

client.coffee:
	Db = require 'db'
	Dom = require 'dom'
	Ui = require 'ui'
	Modal = require 'modal'

	Dom.section !->
		Dom.h4 "You opinion about the following:"
		Ui.bigImg "http://imgs.xkcd.com/comics/voice.png", !->
			Dom.style
				'height': 362
				'width': 281
		Dom.div !->
			if Db.shared.get 'comment'
				Dom.userText Db.shared.get 'comment'
			else
				Dom.userText "*No comment yet*"
		Ui.button "Comment", !->
			Modal.prompt "What do you think about this?", (val) !->
				Server.call 'comment', val
		Ui.button "Remove comment", !->
			Server.call 'removeComment'

server.coffee:	
	Db = require 'db'

	exports.client_comment = (data) !->
		Db.shared.set 'comment', data

	exports.client_removeComment = !->
		Db.shared.set 'comment', null

sync(rpcName, args..., predictFunc?)

Similar to callServer, but the rpc is locally serialized and always retried until successful. When retried at a later time, function references in args may be removed.

In the (optional) prefictFunc function, you can make changes to any (even read-only!) observable variables, which will be visible until we receive an authoritative reply from the server. This can be used to give immediate UI feedback, even when the user is on a slow connection.

Example:

client.coffee:

	Db = require 'db'
	Dom = require 'dom'
	Ui = require 'ui'
	Modal = require 'modal'

	Dom.section !->
		Dom.h4 "A shared list"
		Ui.list !->
			Db.shared.iterate 'items', (myItem) !->
				Ui.item !->
					Dom.text myItem.get()
			Ui.item !->
				Ui.bigButton "Add", !->
					Modal.prompt "Add a new Item", (val) !->
						Server.sync 'add', val, !->
							Db.shared.set 'items', val #we add the value clientside as mock. It will be set properly by the server.

server.coffee:
	Db = require 'db'

	exports.client_add = (data) !->
		pos = Db.shared.incr 'itemsLength' #we keep track of how many items we have in the list.
		Db.shared.set 'items', pos, data #add entree to the list

send(rpcName, args...)

Exactly the same as call, except for the blocking overlay.

Example:

client.coffee:

	Dom = require 'dom'
	Server = require 'server'
	Ui = require 'ui'

	Dom.section !->
		Ui.bigButton "Send a message to the server, and keep going.", !->
			Server.send("poke", "The cat's out of the bag.")

server.coffee:
	Db = require 'db'

	exports.client_poke = (data) !->
		log "received poke: " + data

Basic topics

API reference

  • API Reference
    • Client
      • [client plugin](client plugin)
      • [client dom](client dom)
      • [client obs](client obs)
      • [client db](client db)
      • [client server](client server)
      • [client page](client page)
      • [client ui](client ui)
      • [client form](client form)
      • [client icon](client icon)
      • [client modal](client modal)
      • [client photo](client photo)
      • [client photoview](client photoview)
      • [client time](client time)
      • [client share](client share)
      • [client map](client map)
      • [client geoloc](client geoloc)
    • Server
      • [server event](server event)
      • [server plugin](server plugin)
      • [server http](server http)
      • [server db](server db)
      • [server photo](server photo)
      • [server time](server time)
  • Example UI elements

Advanced topics

Clone this wiki locally