Websocket library

Contents

Websocket functions.

Usage

Add the following to your build.gradle file:

dependencies {
  include xplibs.websocket
}

Add the import statement to your code:

import webSocketLib from '/lib/xp/websocket';

You are now ready to use the API.

Functions

addToGroup

Adds an id to a socket group.

Takes two positional arguments: group (string — group name) and id (string — socket id).

Returns

void

Example

import {addToGroup} from '/lib/xp/websocket';

addToGroup('people', session.id);

removeFromGroup

Removes an id from a socket group.

Takes two positional arguments: group (string — group name) and id (string — socket id).

Returns

void

Example

import {removeFromGroup} from '/lib/xp/websocket';

removeFromGroup('people', session.id);

send

Sends a message directly to a socket id.

Takes two positional arguments: id (string — socket id) and message (string — text message).

Returns

void

Example

import {send} from '/lib/xp/websocket';

send(session.id, 'You said - ' + message);

sendToGroup

Sends a message to all sockets in group.

Takes two positional arguments: group (string — group name) and message (string — text message).

Returns

void

Example

import {sendToGroup} from '/lib/xp/websocket';

sendToGroup('people', 'Notice this message!');

getGroupSize

Gets number of all sockets in a group.

Calculating size of a group has a linear cost from total number of sockets. Consider using this method only if building a message is a resource consuming operation, otherwise it is better to simply sendToGroup.

Takes one positional argument: group (string — group name).

Returns

number

Example

import {getGroupSize} from '/lib/xp/websocket';

getGroupSize('people');

Contents

Contents