Class: SocketNamespace

socket/SocketNamespace~SocketNamespace()

SocketNamespace

Constructor

new SocketNamespace()

Defines socket.io namespace wrapper.

SocketNamespace wraps a socket.io namespace to be able to connect the namespace to the server after adding handlers to it. Creating a Namespace using socket.io can't be done without creating the server and attaching the namespace to it.

Source:
Example
var openVeoApi = require('@openveo/api');
var namespace = new openVeoApi.socket.SocketNamespace();
var server = new openVeoApi.socket.SocketServer();

// Add a middleware
namespace.use(function(socket, next) {
  console.log('Called for every message');
});

// Listen to a message
namespace.on('test.message', function(data) {
  console.log('test.message received');
  console.log(data);
});

// Add namespace to server
server.addNamespace('/myName', namespace);

// Start server
server.listen(80, function() {
  console.log('Socket server started');
  namespace.emit('test.message', 'some data');
});

Members

(readonly) handlers :Object

The list of messages' handlers.

Type:
  • Object
Source:

(readonly) middlewares :Array

The list of middlewares.

Type:
  • Array
Source:

(readonly) namespace :Namespace

The socket namespace.

Type:
  • Namespace
Source:

Methods

(private, static) mountHandlers()

Mounts the namespace's handlers on socket.io namespace.

This:
Source:

(private, static) mountMiddlewares()

Mounts the namespace's middlewares on socket.io namespace.

This:
Source:

emit(message, data)

Emits a message to all clients connected to the namespace.

It will work only if the socket server is started.

Parameters:
Name Type Description
message String

The message to send to clients

data *

The data to send to clients

Source:

on(id, handler)

Listens to a socket's message.

Parameters:
Name Type Description
id String

The message id to listen to

handler function

Function to call when receiving the message

Source:

use(middleware) → {module:socket/SocketNamespace~SocketNamespace}

Registers a middleware.

Middleware gets executed for every incoming socket and receives as parameters the socket and a function to optionally defer execution to the next registered middleware.

Parameters:
Name Type Description
middleware function

Function to call when receiving the message

Source:
Returns:

The socket namespace

Type
module:socket/SocketNamespace~SocketNamespace