Clients

JavaScript Client

symple-client API reference

symple-client is the browser and Node.js client. 9KB minified, no media dependencies.

Install

npm install symple-client

Connect

import SympleClient from 'symple-client'

const client = new SympleClient({
  url: 'ws://localhost:4500',
  token: 'optional-auth-token',
  peer: {
    user: 'alice',
    name: 'Alice',
    type: 'person'
  },
  reconnection: true,
  reconnectionDelay: 3000,
  reconnectionAttempts: 0  // 0 = unlimited
})

client.on('connect', () => console.log('online'))
client.connect()

Constructor options

OptionTypeDefaultDescription
urlstringws://localhost:4500Server WebSocket URL
tokenstringundefinedAuth token
peerobject{}Peer identity (user required, name and type optional)
reconnectionbooleantrueAuto-reconnect on disconnect
reconnectionDelaynumber3000Milliseconds between retries
reconnectionAttemptsnumber0Max retries (0 = unlimited)

Methods

connect()

Open a WebSocket connection and authenticate.

disconnect()

Close the connection gracefully. Stops reconnection.

shutdown()

Close the connection and nullify the socket.

send(message, to?)

Send any message. Sets type: 'message' if not specified. Generates an id if missing. Throws if offline.

client.send({
  subtype: 'chat',
  data: { text: 'Hello' }
}, 'bob|session-id')

sendMessage(message, to?)

Shorthand for send() with type: 'message'.

respond(message)

Send a reply -- swaps to and from from the original message.

sendPresence(p?)

Broadcast presence to all rooms. Merges peer data automatically.

join(room)

Join a dynamic room (requires dynamicRooms: true on server).

leave(room)

Leave a room.

addCapability(name, value?)

Add a capability to the local peer. Value defaults to true.

removeCapability(name)

Remove a capability and broadcast presence.

hasCapability(peerId, name)

Check if a remote peer has a capability.

getCapability(peerId, name)

Get the value of a remote peer's capability.

Properties

PropertyTypeDescription
onlinebooleanConnected and authenticated
peerobjectLocal peer data (includes id after auth)
rosterRosterConnected peers
errorstringCurrent error state

Events

EventPayloadDescription
connect-Authenticated successfully
disconnect-Connection closed
reconnect_attemptnumberReconnection attempt number
connect_errorerrorWebSocket error
errorerror, messageServer error or fatal error
messagemsgMessage received
presencemsgPresence update
commandmsgCommand received
eventmsgEvent received
addPeerpeerPeer came online (via roster)
removePeerpeerPeer went offline (via roster)