Requires:
- module:/lib/xp/websocket
- module:/lib/xp/io
- module:/lib/xp/portal
Example
var ws = require('/lib/wsUtil');
Requires
- module:/lib/xp/websocket
- module:/lib/xp/io
- module:/lib/xp/portal
Classes
Methods
(static) addHandlers(event, handler)
Add additional socket event handlers. This is the secondary handlers for the event and they are being pushed to the additional event handlers array
Parameters:
Name | Type | Description |
---|---|---|
event |
string | The name of the socket event |
handler |
function | The handler to bind to the event |
(static) addUserToGroup(id, group, autoCreateopt)
Adds a user to a group if it exists. Create the group if it doesn't exist and the autoCreate flag is set or log an error if not
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
id |
string | The id of the user |
|
group |
string | The name of the group |
|
autoCreate |
boolean |
<optional> |
The flag to create the group if it doesn't exist |
(static) createGroup(name, autoRemoveopt)
Creates a group with given name. If the autoRemove flag is set, if removes a user from the group when user closes connection
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
name |
string | The name of the group |
|
autoRemove |
boolean |
<optional> |
Removes the user from the group on close connection |
(static) expandClient(name, func)
Expand the client library with new functionalities. NOTE: The expansion object have direct access to the client library's inner variables and functions
Parameters:
Name | Type | Description |
---|---|---|
name |
string | object | Name of the new client function or an object with key=name value=function |
func |
function | The function for the new client interface. |
Example
// service/websocket/websocket.js
var ws = require('path/to/wsUtil');
ws.expandClient('hello', function() { send('Hello'); // use the inner send function });
ws.openWebsockets(exports);
// socket.js
var cws = new ExpWs();
cws.hello();
(static) extend(exportObject)
Extends the library functionalities to create reusable extensions
Parameters:
Name | Type | Description |
---|---|---|
exportObject |
object | The object that extends the library |
- Source:
- See:
Example
// lib/extension.js
var ws = require('/lib/wsUtil');
var extensionObject = { extension: extension };
ws.extend(extensionObject);
function extension() {
ws.addHandler('open', function(event) { log.info('Extension says hi!'); });
}
exports.extension = ws;
// service/extensionService/extensionService.js
var extensionLib = require('extension').extension
extensionLib.openWebsockets(exports);
extensionLib.extension(); // Activate extension
(static) getGroupUsers(group) → (nullable) {Array.<string>}
Get all connected users from a group
Parameters:
Name | Type | Description |
---|---|---|
group |
string | Name of the group |
Returns:
The array of users or undefined if group doesn't exist
- Type
- Array.<string>
(static) getWsEvents(event)
The function that binds to the exports webSocketEvent method. This function checks the ws event object for type and calls the appropriate handler. Then it loops over the additional handlers and calls each one of them.
Parameters:
Name | Type | Description |
---|---|---|
event |
object | The event sent from the client |
(static) openWebsockets(exp, hostopt)
Opens the websocket connection and delegate events to handlers
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
exp |
object | The exports object from the Enonic module that is assigned to handle websocket events |
||
host |
string |
<optional> |
portal.serviceUrl({ service: 'websocket'}) | The host for the service that serves the web sockets |
(static) removeUserFromGroup(name, id)
Removes a user from a group of given name, if the group is empty, it removes the group
Parameters:
Name | Type | Description |
---|---|---|
name |
string | Name of the group |
id |
string | The id of the user |
(static) send(id, message)
Sends a message to given client, if the message is of type object it will be stringified
Parameters:
Name | Type | Description |
---|---|---|
id |
string | The id of the user |
message |
string | object | The message to send |
(static) sendSocketResponse(req, hostopt) → {SocketResponse}
Send the socket response for a socket request
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
req |
object | The request object passed to the server |
||
host |
string |
<optional> |
portal.serviceUrl({ service: 'websocket'}) | The url to the service that serves the web sockets |
Returns:
The response object
- Type
- SocketResponse
Example
// someSocketService.js
ws = require('/lib/wsUtil');
exports.get = ws.sendSocketResponse;
// or
exports.get = function(req) {
return ws.sendSocketResponse(req);
}
(static) sendToGroup(name, message)
extends the sendToGroup function from EnonicXP to stringify objects
Parameters:
Name | Type | Description |
---|---|---|
name |
string | Name of the group |
message |
object | string | Message to send to the group |
Example
// send to space 'global'
ws.sendToGroup('global', { hello: 'everyone in global' });
(static) setEventHandler(event, handler)
Sets a handler for given socket event
Parameters:
Name | Type | Description |
---|---|---|
event |
'open' | 'close' | 'message' | 'error' | The name of the event |
handler |
function | The event handler |
(static) setEventHandlers(handlers)
Set all socket event handlers
Parameters:
Name | Type | Description |
---|---|---|
handlers |
object |
(static) setSocketRequestResponse(response)
set custom socket response object. Default object is taken from Enonic doc
Parameters:
Name | Type | Description |
---|---|---|
response |
SocketResponse | The socket response object |
Type Definitions
SocketResponse
The response object sent from the server
Type:
- object
Properties:
Name | Type | Description | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
webSocket |
object | Main response object Properties
|