/
broadcast.js
executable file
·78 lines (54 loc) · 2.05 KB
/
broadcast.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
module.exports = {
friendlyName: 'Broadcast',
description: 'Broadcast a message to all connected sockets in the specified room.',
extendedDescription: 'Note that this machine does not wait for any kind of acknowledgement of message delivery, so it is immediate/synchronous.',
habitat: 'sails',
sync: true,
inputs: {
eventName: {
friendlyName: 'Message name',
description: 'The name (aka "event name") to use for the message (just an arbitrary label).',
example: 'news',
required: true
},
data: {
example: '*',
friendlyName: 'Message data',
description: 'Data to send with the message.',
},
// omitRequestor: {
// friendlyName: 'Skip requesting socket?',
// example: true,
// description: 'Do not send the message to the requesting socket.',
// friendlyName: 'Omit requesting socket'
// },
//
roomName: {
friendlyName: 'Room',
example: 'myChatRoom',
description: 'The name of the room where this message will be broadcasted.',
required: true
}
},
fn: function(inputs, exits, env) {
// Import Lodash.
var _ = require('@sailshq/lodash');
// If we don't have a Sails app in our environment, bail through the `error` exit.
if (!_.isObject(env.sails) || env.sails.constructor.name !== 'Sails') {
return exits.error(new Error('A valid Sails app must be provided through `.setEnv()` in order to use this machine.'));
}
// If we don't have the sockets hook enabled in our environment, bail through the `error` exit.
if (!_.isObject(env.sails.sockets)) {
return exits.error(new Error('The `sockets` hook must be enabled on the passed-in Sails app in order to use this machine.'));
}
// Use `sails.sockets.blast` to send the message to
// all sockets subscribed to `inputs.roomName`!
env.sails.sockets.broadcast(
inputs.roomName,
inputs.eventName || 'message',
inputs.data || null
);
// Return through the `success` exit.
return exits.success();
},
};