Skip to content

Commit

Permalink
Merge pull request #325 from pusher/r4.4
Browse files Browse the repository at this point in the history
R4.4
  • Loading branch information
leesio authored Feb 14, 2019
2 parents 8aa6883 + aaaa88a commit 7837cf2
Show file tree
Hide file tree
Showing 29 changed files with 986 additions and 530 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 4.4.0 (2019-02-14)

[NEW] Callbacks bound to client events on presence channels will be called with an extra argument containing the `user_id` of the message sender

[NEW] Warn when trying to trigger client-events to a channel that isn't subscribed

## 4.3.1 (2018-09-03)

[FIXED] Honour protocol error codes received after connection succeeds
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,14 @@ channel.bind('my-event', function () {
}, { name: 'Pusher' });
```

For client-events on presence channels, bound callbacks will be called with an additional argument. This argument is an object containing the `user_id` of the user who triggered the event

```
presenceChannel.bind('client-message', function (data, metadata) {
console.log('received data from', metadata.user_id, ':', data);
});
```

Unsubscribe behaviour varies depending on which parameters you provide it with. For example:

```js
Expand Down
187 changes: 115 additions & 72 deletions dist/node/pusher.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Pusher JavaScript Library v4.3.1
* Pusher JavaScript Library v4.4.0
* https://pusher.com/
*
* Copyright 2017, Pusher
Expand Down Expand Up @@ -119,16 +119,17 @@ module.exports =
_this.timelineSender.send(_this.connection.isUsingTLS());
}
});
this.connection.bind('message', function (params) {
var internal = (params.event.indexOf('pusher_internal:') === 0);
if (params.channel) {
var channel = _this.channel(params.channel);
this.connection.bind('message', function (event) {
var eventName = event.event;
var internal = (eventName.indexOf('pusher_internal:') === 0);
if (event.channel) {
var channel = _this.channel(event.channel);
if (channel) {
channel.handleEvent(params.event, params.data);
channel.handleEvent(event);
}
}
if (!internal) {
_this.global_emitter.emit(params.event, params.data);
_this.global_emitter.emit(event.event, event.data);
}
});
this.connection.bind('connecting', function () {
Expand Down Expand Up @@ -833,7 +834,7 @@ module.exports =

"use strict";
var Defaults = {
VERSION: "4.3.1",
VERSION: "4.4.0",
PROTOCOL: 7,
host: 'ws.pusherapp.com',
ws_port: 80,
Expand Down Expand Up @@ -1079,15 +1080,21 @@ module.exports =
this.unbind_global();
return this;
};
Dispatcher.prototype.emit = function (eventName, data) {
var i;
for (i = 0; i < this.global_callbacks.length; i++) {
Dispatcher.prototype.emit = function (eventName, data, metadata) {
for (var i = 0; i < this.global_callbacks.length; i++) {
this.global_callbacks[i](eventName, data);
}
var callbacks = this.callbacks.get(eventName);
var args = [];
if (metadata) {
args.push(data, metadata);
}
else if (data) {
args.push(data);
}
if (callbacks && callbacks.length > 0) {
for (i = 0; i < callbacks.length; i++) {
callbacks[i].fn.call(callbacks[i].context || global, data);
for (var i = 0; i < callbacks.length; i++) {
callbacks[i].fn.apply(callbacks[i].context || global, args);
}
}
else if (this.failThrough) {
Expand Down Expand Up @@ -1793,6 +1800,9 @@ module.exports =
},
javascriptQuickStart: {
path: "/docs/javascript_quick_start"
},
triggeringClientEvents: {
path: "/docs/client_api_guide/client_events#trigger-events"
}
}
};
Expand Down Expand Up @@ -2320,30 +2330,35 @@ module.exports =
/***/ (function(module, exports) {

"use strict";
exports.decodeMessage = function (message) {
exports.decodeMessage = function (messageEvent) {
try {
var params = JSON.parse(message.data);
if (typeof params.data === 'string') {
var messageData = JSON.parse(messageEvent.data);
var pusherEventData = messageData.data;
if (typeof pusherEventData === 'string') {
try {
params.data = JSON.parse(params.data);
}
catch (e) {
if (!(e instanceof SyntaxError)) {
throw e;
}
pusherEventData = JSON.parse(messageData.data);
}
catch (e) { }
}
var pusherEvent = {
event: messageData.event,
channel: messageData.channel,
data: pusherEventData
};
if (messageData.user_id) {
pusherEvent.user_id = messageData.user_id;
}
return params;
return pusherEvent;
}
catch (e) {
throw { type: 'MessageParseError', error: e, data: message.data };
throw { type: 'MessageParseError', error: e, data: messageEvent.data };
}
};
exports.encodeMessage = function (message) {
return JSON.stringify(message);
exports.encodeMessage = function (event) {
return JSON.stringify(event);
};
exports.processHandshake = function (message) {
message = exports.decodeMessage(message);
exports.processHandshake = function (messageEvent) {
var message = exports.decodeMessage(messageEvent);
if (message.event === "pusher:connection_established") {
if (!message.data.activity_timeout) {
throw "No activity timeout specified in handshake";
Expand Down Expand Up @@ -2435,12 +2450,12 @@ module.exports =
return this.transport.send(data);
};
Connection.prototype.send_event = function (name, data, channel) {
var message = { event: name, data: data };
var event = { event: name, data: data };
if (channel) {
message.channel = channel;
event.channel = channel;
}
logger_1["default"].debug('Event sent', message);
return this.send(Protocol.encodeMessage(message));
logger_1["default"].debug('Event sent', event);
return this.send(Protocol.encodeMessage(event));
};
Connection.prototype.ping = function () {
if (this.transport.supportsPing()) {
Expand All @@ -2456,23 +2471,23 @@ module.exports =
Connection.prototype.bindListeners = function () {
var _this = this;
var listeners = {
message: function (m) {
var message;
message: function (messageEvent) {
var pusherEvent;
try {
message = Protocol.decodeMessage(m);
pusherEvent = Protocol.decodeMessage(messageEvent);
}
catch (e) {
_this.emit('error', {
type: 'MessageParseError',
error: e,
data: m.data
data: messageEvent.data
});
}
if (message !== undefined) {
logger_1["default"].debug('Event recd', message);
switch (message.event) {
if (pusherEvent !== undefined) {
logger_1["default"].debug('Event recd', pusherEvent);
switch (pusherEvent.event) {
case 'pusher:error':
_this.emit('error', { type: 'PusherError', data: message.data });
_this.emit('error', { type: 'PusherError', data: pusherEvent.data });
break;
case 'pusher:ping':
_this.emit("ping");
Expand All @@ -2481,7 +2496,7 @@ module.exports =
_this.emit("pong");
break;
}
_this.emit('message', message);
_this.emit('message', pusherEvent);
}
},
activity: function () {
Expand Down Expand Up @@ -2619,18 +2634,26 @@ module.exports =
callback(error, authData);
});
};
PresenceChannel.prototype.handleEvent = function (event, data) {
switch (event) {
PresenceChannel.prototype.handleEvent = function (event) {
var eventName = event.event;
if (eventName.indexOf("pusher_internal:") === 0) {
this.handleInternalEvent(event);
}
else {
var data = event.data;
var metadata = {};
if (event.user_id) {
metadata.user_id = event.user_id;
}
this.emit(eventName, data, metadata);
}
};
PresenceChannel.prototype.handleInternalEvent = function (event) {
var eventName = event.event;
var data = event.data;
switch (eventName) {
case "pusher_internal:subscription_succeeded":
this.subscriptionPending = false;
this.subscribed = true;
if (this.subscriptionCancelled) {
this.pusher.unsubscribe(this.name);
}
else {
this.members.onSubscription(data);
this.emit("pusher:subscription_succeeded", this.members);
}
this.handleSubscriptionSucceededEvent(event);
break;
case "pusher_internal:member_added":
var addedMember = this.members.addMember(data);
Expand All @@ -2642,8 +2665,17 @@ module.exports =
this.emit('pusher:member_removed', removedMember);
}
break;
default:
private_channel_1["default"].prototype.handleEvent.call(this, event, data);
}
};
PresenceChannel.prototype.handleSubscriptionSucceededEvent = function (event) {
this.subscriptionPending = false;
this.subscribed = true;
if (this.subscriptionCancelled) {
this.pusher.unsubscribe(this.name);
}
else {
this.members.onSubscription(event.data);
this.emit("pusher:subscription_succeeded", this.members);
}
};
PresenceChannel.prototype.disconnect = function () {
Expand Down Expand Up @@ -2696,6 +2728,7 @@ module.exports =
var dispatcher_1 = __webpack_require__(14);
var Errors = __webpack_require__(46);
var logger_1 = __webpack_require__(16);
var url_store_1 = __webpack_require__(30);
var Channel = (function (_super) {
__extends(Channel, _super);
function Channel(name, pusher) {
Expand All @@ -2715,27 +2748,35 @@ module.exports =
if (event.indexOf("client-") !== 0) {
throw new Errors.BadEventName("Event '" + event + "' does not start with 'client-'");
}
if (!this.subscribed) {
var suffix = url_store_1["default"].buildLogSuffix("triggeringClientEvents");
logger_1["default"].warn("Client event triggered before channel 'subscription_succeeded' event . " + suffix);
}
return this.pusher.send_event(event, data, this.name);
};
Channel.prototype.disconnect = function () {
this.subscribed = false;
this.subscriptionPending = false;
};
Channel.prototype.handleEvent = function (event, data) {
if (event.indexOf("pusher_internal:") === 0) {
if (event === "pusher_internal:subscription_succeeded") {
this.subscriptionPending = false;
this.subscribed = true;
if (this.subscriptionCancelled) {
this.pusher.unsubscribe(this.name);
}
else {
this.emit("pusher:subscription_succeeded", data);
}
}
Channel.prototype.handleEvent = function (event) {
var eventName = event.event;
var data = event.data;
if (eventName === "pusher_internal:subscription_succeeded") {
this.handleSubscriptionSucceededEvent(event);
}
else if (eventName.indexOf("pusher_internal:") !== 0) {
var metadata = {};
this.emit(eventName, data, metadata);
}
};
Channel.prototype.handleSubscriptionSucceededEvent = function (event) {
this.subscriptionPending = false;
this.subscribed = true;
if (this.subscriptionCancelled) {
this.pusher.unsubscribe(this.name);
}
else {
this.emit(event, data);
this.emit("pusher:subscription_succeeded", event.data);
}
};
Channel.prototype.subscribe = function () {
Expand All @@ -2747,7 +2788,7 @@ module.exports =
this.subscriptionCancelled = false;
this.authorize(this.pusher.connection.socket_id, function (error, data) {
if (error) {
_this.handleEvent('pusher:subscription_error', data);
_this.emit('pusher:subscription_error', data);
}
else {
_this.pusher.send_event('pusher:subscribe', {
Expand Down Expand Up @@ -2949,12 +2990,14 @@ module.exports =
EncryptedChannel.prototype.trigger = function (event, data) {
throw new Errors.UnsupportedFeature('Client events are not currently supported for encrypted channels');
};
EncryptedChannel.prototype.handleEvent = function (event, data) {
if (event.indexOf("pusher_internal:") === 0 || event.indexOf("pusher:") === 0) {
_super.prototype.handleEvent.call(this, event, data);
EncryptedChannel.prototype.handleEvent = function (event) {
var eventName = event.event;
var data = event.data;
if (eventName.indexOf("pusher_internal:") === 0 || eventName.indexOf("pusher:") === 0) {
_super.prototype.handleEvent.call(this, event);
return;
}
this.handleEncryptedEvent(event, data);
this.handleEncryptedEvent(eventName, data);
};
EncryptedChannel.prototype.handleEncryptedEvent = function (event, data) {
var _this = this;
Expand Down
Loading

0 comments on commit 7837cf2

Please sign in to comment.