-
-
Notifications
You must be signed in to change notification settings - Fork 261
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
base: main
Are you sure you want to change the base?
Conversation
d43431f
to
014a36f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const url = window.location.origin | ||
.replace("https://", "wss://") | ||
.replace("http://", "ws://"); | ||
|
||
const client = new ActionheroWebsocketClient(url, { | ||
cookieKey: "sessionID", | ||
}); |
There was a problem hiding this comment.
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
client.onDisconnect = () => { | ||
console.log("Disconnected!"); | ||
}; | ||
|
||
client.onSay = (message) => { | ||
appendMessage(message); | ||
}; | ||
|
||
client.onWelcome = (message) => { | ||
appendMessage(message); | ||
}; |
There was a problem hiding this comment.
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
try { | ||
await client.action("createChatRoom", { name: "defaultRoom" }); | ||
} catch (error) { | ||
if (error.message !== "room exists") throw error; | ||
} |
There was a problem hiding this comment.
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!
<script type="module"> | ||
import { ActionheroWebsocketClient } from "../clients/websocket.js"; |
There was a problem hiding this comment.
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
{ | ||
"compilerOptions": { | ||
"outDir": "./public/clients", | ||
"allowJs": true, | ||
"target": "es2020", | ||
"lib": ["es2020", "DOM"], | ||
"module": "ES2020", | ||
"sourceMap": true, | ||
"allowSyntheticDefaultImports": true, | ||
"declaration": true, | ||
"types": [] | ||
}, | ||
"include": ["./src/clients/**/*"] | ||
} |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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!
Closes #1946
The goals of this exploration are to:
primus
and just usews
on the server and the browser's built-inWebSocket
library. It's 2021!ActionHeroWebSocketClient
that can be included in React and Angular projectsActionHeroWebSocketClient.js
that can still be used by "static"/"simple" HTML projects (eg: the samplechat.js
that ships with Actionhero