Proposal: handle onMessage by message type
See original GitHub issueIssue Description
This would be a big breaking change, I’d like to know your thoughts and opinions about this. Will it be valuable/worth doing? Feedback is highly appreciated!
Server-side
client.send()
to replace this.send()
+ message type
:
// new
client.send("skill", { name: "fireball" })
// deprecate
this.send(client, { type: "skill", name: "fireball" }
client.send()
a schema-encoded message
This is already supported by the server-side. See below plans to support this from client-side as well.
class SkillMessage extends Schema {
@type("string") name: string;
}
const skill = new SkillMessage();
skill.name = "fireball";
client.send(skill);
Broadcast with type + data
// new
this.broadcast("skill", { name: "fireball" })
// deprecate
this.broadcast({ type: "skill", name: "fireball" })
onMessage
to handle messages sent from the clients
// new
onCreate() {
this.onMessage("move", (client, message) => {
// handle "move" message
});
this.onMessage(MoveMessage, (client, message) => {
// handle schema-encoded "SchemaMessage"
// (get autocompletion for "message")
});
}
// deprecate
onMessage(client, message) {
}
Client-side
Allow sending the type of a message along with its data:
// new
room.send("move", { angle: 270 });
// deprecate
room.send({ type: "move", angle: 170 });
Allow sending a Schema structure from the client-side (this will be specially useful for strongly-typed languages, such as C#, C++, etc.)
class MoveMessage extends Schema {
@type("number") angle: number;
}
const message = new MoveMessage();
message.angle = 170;
room.send(message);
Receiving messages by type
:
// new
room.onMessage("skill", (message) => {
// handle "skill" message
});
room.onMessage(SkillMessage, (message) => {
// handle schema-encoded "skill" message
});
// deprecate
room.onMessage((message) => {
// handle any type of message
});
As always suggestions are welcome.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:7
- Comments:20 (6 by maintainers)
Top Results From Across the Web
ADM Message Types - Amazon Developer
The ADM SDK handles your messages with the onMessage method. The behavior of ADM messages on a device depends on the state of...
Read more >Design pattern for handling multiple message types
The basic idea is to register a set of handlers that dispatch the received messages to the handler for processing based on the...
Read more >About FCM messages | Firebase Cloud Messaging - Google
With FCM, you can send two types of messages to clients: Notification messages, sometimes thought of as "display messages." These are handled by...
Read more >Interaction Patterns - Documentation - Akka
This ensures that only the right kind of messages can be sent to an actor and ... adapt the response message to a...
Read more >Creating a chat application with WebRTC - LogRocket Blog
ws.on("message", msg => { .... const { type, name, offer } = data; //Handle message by type switch (type) { .... case "offer":...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I like this change.
It sounds like an amazing change, and I would more like to use Schema instead of Fossil Delta. Why I choose Fossil Delta but not Schema? Because the schema code generator is not customable. Would you consider to implement the generator by template mechanism?