Constructor
new SocketEmitter() → {SocketEmitterInterface}
Returns:
Interface for handling incoming sockets
Example
// Simple chat example
var socketEmitter = new ws.SocketEmitter(); // create instance
var motd = 'Welcome to our chat';
var users = {};
socketEmitter.connect(connectionCallback);
function connectionCallback(socket) {
socket.emit('motd', motd); // Send message of the day
socket.on('register-username', function(username) { // Register username
if (!users[username]) { // if not taken
users[username] = socket.id; // Register the new user
socket.emit('username', 'ok'); // Tell the client that username is ok
socket.broadcast('user-enter', username); // Tell all clients that a new user has entered the chat
}
else { // if taken
socket.emit('username', 'taken'); // Tell the client to chose again
}
});
socket.on('public-message', function(message) {
socketEmitter.broadcast('public-message', message); // Broadcast public messages
});
socket.on('private-message', function(message) {
socket.sendTo(users[message.username], message.content); // Send private message with a simple user lookup
});
socket.on('disconnect', function() { // Clean up stuff when user leaves
for (var username in users) { // Find the correct user id
if (users.hasOwnProperty(username) && users[username] === socket.id) {
delete users[username]; // remove the user
socketEmitter.broadcast('user-leave', username); // Broadcast that a user has left the chat
}
}
});
}
Classes
Members
(private, inner) _cb
Properties:
Name | Type | Description |
---|---|---|
_cb |
ConnectionCallback | The callback function to call when a user connects |
(private, inner) _handlers
Properties:
Name | Type | Description |
---|---|---|
_handlers |
EmitterHandlers | Handlers for all users and events |
(private, inner) _users
Properties:
Name | Type | Description |
---|---|---|
_users |
EmitterUsers | Users that has a socket connection |
Methods
(static) broadcast(event, object)
Emit an event to every connected users
Parameters:
Name | Type | Description |
---|---|---|
event |
string | The name of the event |
object |
object | string | The message of the emit event |
(static) connect(callback)
This is the event that is being called when a new user connects
Parameters:
Name | Type | Description |
---|---|---|
callback |
ConnectionCallback | The callback to call when users connects |
Type Definitions
ConnectionCallback(socket)
Callback function for connected users, this is being called with the connected user interface
Parameters:
Name | Type | Description |
---|---|---|
socket |
EmitterUsers | The connected user interface |
Example
socketEmitter.connect(connectionCallback);
function connectionCallback(socket) {
// do something with connected socket
}
EmitterHandlers
Type:
- object
Properties:
Name | Type | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
id |
object | Id of the user Properties
|
EmitterUsers
Type:
- object
Properties:
Name | Type | Description |
---|---|---|
id |
string | The session id of the user |
on |
wsUtil.SocketEmitter~On | The client emitted event handler interface |
emit |
wsUtil.SocketEmitter~Emit | The server side emit message functionality, |
sendTo |
function | The server side send message to specific user functionality |
SocketEmitterInterface
Call the SocketEmitter constructor to receive a SocketEmitter interface Use the interface to act on connection events from clients and broadcast events from the server
Type:
- object
Properties:
Name | Type | Description |
---|---|---|
connect |
ConnectionCallback | A event that fires every time a user connects to the web socket |
broadcast |
function | Emit an event to all connected users |
Example
// Create the SocketEmitter
var socketEmitter = new ws.SocketEmitter();
// Get connection events
socketEmitter.connect(function(socket) {
// Use the client socket connection to emit messages
socket.emit('new-connection', { greetings: 'Hello from server' });
// Use the client socket connection to listen to emitted events from user
socket.on('some-event', function(message) {
log.info('some-event message ' + JSON.stringify(message));
});
});
// Broadcast messages to all users
socketEmitter.broadcast('hello-everybody', { message: 'Hello from server' });