Websocket API

Contents

Websocket functions.

Usage

Add the following to your build.gradle file:

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

Add the import statement to your controller:

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

You are now ready to use the API.

Functions

addToGroup

Adds an id to a socket group.

Parameters

Name Type Description

group

string

Group name

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.

Parameters

Name Type Description

group

string

Group name

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.

Parameters

Name Type Description

id

string

Socket id

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.

Parameters

Name Type Description

group

string

Group name

message

string

Text message

Returns

void

Example

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

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

getGroupSize

XP XP 7.6.0 7.6.0 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.

Parameters

Name Type Description

group

string

Group name

Returns

number

Example

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

getGroupSize('people');

Contents

Contents

AI-powered search

Juke AI