Websocket Library

Contents

Websocket Library

Websocket functions.

Usage

Add the following into 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 free 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!');

Contents