Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade Sugar to Primus 8 #185

Merged
merged 1 commit into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions lib/SugarClient/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
class SugarClient {
static initClass() {
this.host = null;
this.Primus = null;
}

constructor(userId, authToken) {
this.primusUrl = this.primusUrl.bind(this);
this.connect = this.connect.bind(this);
this.disconnect = this.disconnect.bind(this);
this.receiveData = this.receiveData.bind(this);
this.subscribeTo = this.subscribeTo.bind(this);
this.unsubscribeFrom = this.unsubscribeFrom.bind(this);
this.on = this.on.bind(this);
this.off = this.off.bind(this);
this.emit = this.emit.bind(this);
this.__subscribeToChannels = this.__subscribeToChannels.bind(this);
this.__subscribeTo = this.__subscribeTo.bind(this);
this.createEvent = this.createEvent.bind(this);
this.userId = userId;
this.authToken = authToken;
this.events = { };
this.subscriptions = { };
this.initializePrimus();
}

initializePrimus() {
if (SugarClient.Primus == null) { throw 'SugarClient.Primus is not defined'; }
if (SugarClient.host == null) { throw 'SugarClient.host is not defined'; }
this.primus = SugarClient.Primus.connect(SugarClient.host, {
websockets: true,
network: true,
manual: true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only difference between this and https://github.com/zooniverse/Sugar-Client/blob/master/client.js that I'm seeing is the deletion of ping: 10000 (link). Apologies if it has already been stated somewhere, but is that property no longer needed with Primus 8, and it was with Primus 6?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s been removed because the direction of the ping/pong protocol changed from client->server to server->client. There’s a list of client options in the Readme.

Copy link
Contributor Author

@eatyourgreens eatyourgreens Jan 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So that option said: ping the server then wait 10s for a pong response. The client now sends pong back in response to pings from the server.

You can see the traffic if you watch the web socket in browser dev tools.

}
);

this.primus.on('outgoing::url', this.primusUrl);
return this.primus.on('data', this.receiveData);
}

host() {
return SugarClient.host;
}

primusUrl(baseUrl) {
if (this.userId && this.authToken) {
return baseUrl.query = `user_id=${ this.userId }&auth_token=${ this.authToken }`;
}
}

connect() {
this.disconnect();
return this.primus.open();
}

disconnect() {
let key;
let userKeys = [];
userKeys = ((() => {
const result = [];
for (key in this.subscriptions) {
const _ = this.subscriptions[key];
if (key.match(/^(session|user):/i)) {
result.push(key);
}
}
return result;
})());
for (key of userKeys) { delete this.subscriptions[key]; }
this.userKey = (this.loggedIn = null);
return this.primus.end();
}

receiveData(data) {
if (data.type === 'connection') {
if (console && console.info) {
console.info('[CONNECTED] ', data);
}
this.loggedIn = data.loggedIn;
this.userKey = data.userKey;
this.subscriptions[this.userKey] = true;
return setTimeout(this.__subscribeToChannels, 100);
} else {
return this.emit(data);
}
}

subscribeTo(channel) {
if (this.subscriptions[channel]) { return false; }
this.subscriptions[channel] = true;
return this.__subscribeTo(channel);
}

unsubscribeFrom(channel) {
if (!this.subscriptions[channel]) { return; }
delete this.subscriptions[channel];
return this.primus.write({action: 'Unsubscribe', params: { channel }});
}

on(type, callback) {
if (!this.events[type]) { this.events[type] = []; }
return this.events[type].push(callback);
}

off(type, callback) {
if (callback && this.events[type]) {
return this.events[type] = this.events[type].filter(cb => cb !== callback);
} else {
return delete this.events[type];
}
}

emit(data) {
const callbacks = this.events[data.type] || [];
return callbacks.map((callback) => callback(data));
}

__subscribeToChannels() {
return (() => {
const result = [];
for (let channel in this.subscriptions) {
const _ = this.subscriptions[channel];
result.push(this.__subscribeTo(channel));
}
return result;
})();
}

__subscribeTo(channel) {
return this.primus.write({action: 'Subscribe', params: { channel }});
}

createEvent(type, channel, data) {
return this.primus.write({
action: 'Event',
params: { type, channel, data }});
}
}
SugarClient.initClass();

if (typeof module !== 'undefined' && module !== null) {
module.exports = SugarClient;
}
Loading