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

Modern websocket client & Server #1949

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft

Modern websocket client & Server #1949

wants to merge 7 commits into from

Conversation

evantahler
Copy link
Member

Closes #1946

The goals of this exploration are to:

  • Remove primus and just use ws on the server and the browser's built-in WebSocket library. It's 2021!
  • Provide a typed ActionHeroWebSocketClient that can be included in React and Angular projects
  • Provide a compiled ActionHeroWebSocketClient.js that can still be used by "static"/"simple" HTML projects (eg: the sample chat.js that ships with Actionhero

@evantahler evantahler marked this pull request as draft September 8, 2021 00:43
@evantahler evantahler changed the title Modern ws client & Server Modern websocket client & Server Sep 8, 2021
@evantahler evantahler added BREAKING-CHANGE enhancement New feature or request labels Sep 8, 2021
Copy link
Member Author

@evantahler evantahler left a comment

Choose a reason for hiding this comment

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

@mfvargo and @gcoonrod - here's a not-fully-working yet proposal for modernizing the websocket client library. What do you think?

Comment on lines +61 to +67
const url = window.location.origin
.replace("https://", "wss://")
.replace("http://", "ws://");

const client = new ActionheroWebsocketClient(url, {
cookieKey: "sessionID",
});
Copy link
Member Author

Choose a reason for hiding this comment

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

ActionheroWebsocketClient would now require some options, eg the URL and cookie settings

Comment on lines +73 to +83
client.onDisconnect = () => {
console.log("Disconnected!");
};

client.onSay = (message) => {
appendMessage(message);
};

client.onWelcome = (message) => {
appendMessage(message);
};
Copy link
Member Author

Choose a reason for hiding this comment

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

Looks like we still don't have EventEmitter or something like it in the browser that can handle custom payloads

Comment on lines +87 to +91
try {
await client.action("createChatRoom", { name: "defaultRoom" });
} catch (error) {
if (error.message !== "room exists") throw error;
}
Copy link
Member Author

Choose a reason for hiding this comment

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

All Async/Await now, even in the browser!

Comment on lines +58 to +59
<script type="module">
import { ActionheroWebsocketClient } from "../clients/websocket.js";
Copy link
Member Author

Choose a reason for hiding this comment

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

We can don dynamic imports in the browser with JS modules

Comment on lines +1 to +14
{
"compilerOptions": {
"outDir": "./public/clients",
"allowJs": true,
"target": "es2020",
"lib": ["es2020", "DOM"],
"module": "ES2020",
"sourceMap": true,
"allowSyntheticDefaultImports": true,
"declaration": true,
"types": []
},
"include": ["./src/clients/**/*"]
}
Copy link
Member Author

Choose a reason for hiding this comment

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

The client lib has a different ES target than the rest of the project

data: DataType;
};

export class ActionheroWebsocketClient {
Copy link
Member Author

Choose a reason for hiding this comment

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

A new, strongly typed TS class for ActionheroWebsocketClient


export const DEFAULT = {
servers: {
websocket: (config) => {
websocket: () => {
return {
enabled: true,
Copy link
Member Author

Choose a reason for hiding this comment

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

Almsot no server options now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BREAKING-CHANGE enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ActionheroWebsocketClient typescript support
1 participant