Typed pub / sub, request / responses for web apps using Socket.io and Zod, inspired by ROS.
Documentation (https://shiukaheng.github.io/WebTopics/)
const SensorTopic = createTopic("sensor", z.object({
"temperature": z.number(),
"humidity": z.number()
}))
client.pub(SensorTopic, {temperature: 20, humidity: "50%"}) // Error: Expected number, received string
client1.pub(channel, {a: "1"})
client2.pub(channel, {b: "2"}) // New state merged on old by default
client3.sub(channel, (value) => {
console.log(value) // {a: "1", b: "2"}
})
client2.pub(channel, {b: "2"}, true) // Or publishing as an overwrite
// client 3 receives: {b: "2"}
const AdditionService = createService("add",
// Request schema
z.object({
"a": z.number(),
"b": z.number()
}),
// Response schema
z.number()
)
...
// Call service
const result = await client.req(AdditionService, someClientID, {a: 1, b: 2}) // Promise<number>
export const AdditionService = createService("add",
z.object({
"a": z.number(),
"b": z.number()
}),
z.number()
)
export const SensorTopic = createTopic("sensor", z.object({
"temperature": z.number(),
"humidity": z.number()
}))
...
// Client and server can share the same channels from an external package
import {AdditionService, SensorTopic} from "channels"
Generated using TypeDoc