Websocket Library

Contents

Websocket functions.

Usage

Add the following to your build.gradle file:

dependencies {
  include "com.enonic.xp:lib-websocket:${xpVersion}"
}

In your JavaScript controller, add a require statement:

var webSocketLib = require('/lib/xp/websocket');

You are now ready to use the library functionality.

Functions

addToGroup

Add an id to a socket group.

Parameters

Name Type Description

group

string

Group name

id

string

Socket id

Returns

void

Example

webSocketLib.addToGroup('people', session.id);

removeFromGroup

Remove an id from a socket group.

Parameters

Name Type Description

group

string

Group name

id

string

Socket id

Returns

void

Example

webSocketLib.removeFromGroup('people', session.id);

send

Send message directly to a socket id.

Parameters

Name Type Description

id

string

Socket id

message

string

Text message

Returns

void

Example

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

sendToGroup

Send message to all sockets in group.

Parameters

Name Type Description

group

string

Group name

message

string

Text message

Returns

void

Example

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

getGroupSize

XP XP 7.6.0 7.6.0 Get number of all sockets in 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.

Parameters

Name Type Description

group

string

Group name

Returns

number

Example

webSocketLib.getGroupSize('people');

Contents

Contents