Constructor
new QueueWebSocket()
The objective of the WebSocket queue is to perform client to client real-time communication based on the EventEmitter model.
client A <-> server <-> client B
The server is the man in the middle.
Each client is subscribing functions to a specific topic. The server is broadcasting to any client which have explicitly subscribed their interest to the message topic.
The communication can go:
- from the client to the server
- from a client to subscribed clients
- from the server to subscribed clients
- Source:
- See:
- To Do:
-
- How to allow vertical scalability between websocket servers and clients When all servers are listening on a specific event and send data to their client, the scalability is validated. However, when a client is sending a message to the server, this message must be propagated to each WebSocket as well. A solution can be to use QueueAmqp or QueueRedis for this.
Example
const QueueWebSocket = require('QueueWebSocket');
const server = new QueueWebSocket({
name: 'server',
server: true
});
const alice = new QueueWebSocket({
name: 'Alice'
});
const bernard = new QueueWebSocket({
name: 'Bernard'
});
// Server and clients connection:
yield server.connect();
yield alice.connect();
yield bernard.connect();
alice.on('bernard', msg => {
console.log('Message coming from Bernard: ' + msg);
});
bernard.on('alice', msg => {
console.log('Message coming from Alice: ' + msg);
});
alice.emit('alice', 'Hello Bernard !');
bernard.emit('bernard', 'Hello Alice !');
// Message coming from Alice: Hello Bernard !
// Message coming from Bernard: Hello Alice !
Extends
Members
address
Return the formatted client address to listen to.
- Source:
isServer
- Source:
WEBSOCKET_PORT
- Source:
WEBSOCKET_RECONNECT_MAX_ATTEMPTS
- Source:
WEBSOCKET_RECONNECT_TIMEOUT
- Source:
Methods
(static) portInUse(port, callback)
Detect whether the port is in use.
Parameters:
Name | Type | Description |
---|---|---|
port |
number | to test |
callback |
Callback |
- Source:
broadcast(message, topic) → {QueueWebSocket}
Broadcast a message on every connected clients. Only a WebSocket server can broadcast messages to clients.
Parameters:
Name | Type | Description |
---|---|---|
message |
String | Message to send |
topic |
String | Topic to use to broadcast on |
- Source:
Returns:
The queue used to broadcast
- Type
- QueueWebSocket
close() → {QueueWebSocket}
Close the websocket safely
- Source:
Returns:
The queue to be closed
- Type
- QueueWebSocket
connect() → {Promise}
Connect the WebSocket queue.
- Overrides:
- Source:
Returns:
- True when connected
- Type
- Promise
connectServer() → {Promise}
Create the WebSocket server
- Source:
Returns:
Resolved
- Type
- Promise
emit() → {QueueWebSocket}
Emit a new message to the queue.
- Source:
Returns:
- The queue on which to emit the message
- Type
- QueueWebSocket
on() → {QueueWebSocket}
Listen to new messages emitted from the queue.
- Source:
Returns:
- The queue to be listened
- Type
- QueueWebSocket
onClose() → {QueueWebSocket}
onClose callback when a websocket client or supervisor is disconnected.
- Source:
Returns:
The queue binded to the websocket
- Type
- QueueWebSocket
onMessage(message, flags) → {QueueWebSocket}
Callback called when a message is sent to the QueueWebSocket.
Parameters:
Name | Type | Description |
---|---|---|
message |
String | The message sent |
flags |
Object | List of flags used for the communication |
- Source:
Returns:
The queue binded to the websocket
- Type
- QueueWebSocket
reconnect() → {QueueWebSocket}
Reconnect the client or server queue if disconnected
- Source:
Returns:
The queue binded to the websocket
- Type
- QueueWebSocket