0.1.0 • Published 2 years ago
@kollision/eventhub-tcp-client v0.1.0
import { EventhubTcpClient } from "@kollision/eventhub-tcp-client";
let client = new EventhubTcpClient("myClient", "127.0.0.1");
client.on("event", (channel, event, senderId, senderName) => {
console.log("Received event");
console.log(`- Channel: '${channel}'`);
console.log(`- SenderID: '${senderId}'`);
console.log(`- SenderName: '${senderName}'`);
console.log(`- Event type: '${event.type}'`);
console.log(`- Event data:`, event.data);
});
client.on("open", () => {
console.log("Eventhub client connected!");
// Subscribing to some channels.
// Notice that "foo.bar.baz" is a subchannel of "foo.bar",
// so all events emitted on "foo.bar.baz" will also be
// received on channel "foo.bar"...
client.subscribe("foo.bar.baz");
client.subscribe("foo.bar");
// ... which is why we will receive the following event twice.
// (notice returnToSender: true - otherwise we wouldn't receive our own events)
client.emit("foo.bar.baz", {
type: "hello",
data: {
yes: 23
},
returnToSender: true
});
// we won't receive this event ourselves, since returnToSender isn't true.
client.emit("foo.bar", {
type: "blabla"
})
// we also won't receive this event, since we're not subscribed to channel.
client.emit("some.other.channel", {
type: "some-type",
returnToSender: true
});
// we won't receive the following event at first, since we're also not subscribed to this channel,
// however, we *will* receive it when we do subscribe to the channel, since the event has been marked as sticky.
client.emit("yet.another.channel", {
type: "event-to-be-read-later",
sticky: true,
data: {
message: "dear reader - i am a sticky event, so you will receive me whenever you subscribe to my channel, even long after I have been sent!"
},
returnToSender: true
});
setTimeout(() => {
// now we should receive the event
client.subscribe("yet.another.channel");
}, 5000);
});
client.openConnection();