v0.3.0 notice:
onReceiveMessage/onSendMessageare deprecated and will be removed in v1.0.0. UseaddReceiveMessageListener/addSendMessageListenerinstead.- Breaking: When
target.typeis aRegExp, theoptionsargument is no longer accepted (enforced at the type level; ignored with a warning at runtime). Use astringtypefor native EventAPI options likeonce/signal.- The wildcard form is now
"*"passed as the first argument directly.{ type: "*" }is deprecated and will be removed in v1.0.0.See Migration to v0.3.0 and CHANGELOG.md for details.
Pigeon link is a module for connecting to a Pigeon Room.
const pigeon = new Pigeon({
baseUrl: "wss://your-pigeon-room/pigeon",
address: "address",
staticId: "staticid", // optional
autoReconnect: true, // optional
});Note: with autoReconnect, a new client id is assigned on each reconnect unless
staticId is specified. Re-read it from pigeon.id after reconnect (e.g.
inside addConnectListener).
Handlers can be registered to listen for specific message types, both for receiving and sending messages.
pigeon.addReceiveMessageListener<T>({
type: "messageType",
}, receiveHandler);
// Match every message
pigeon.addReceiveMessageListener("*", receiveHandler);-
target
"*"|{ type: string }|{ type: RegExp }- Specifies which messages to listen for.
"*"matches every message.options(e.g.once) work natively.{ type: string }matches messages whosetypeis exactly equal.optionswork natively.{ type: RegExp }matches messages whosetypematches the pattern. Note: with aRegExp, theoptionsargument is not accepted (the underlying listener does internal filtering, which would consume options likeonceon filtered-out messages). Use a stringtypeor"*"if you need EventAPI options.
-
handler
(message: ReceivedMessage<T>) => voidA callback function that will be invoked when a matching message is received.
- options
- boolean | AddEventListenerOptions — same as the standard
addEventListenerthird argument (once,signal, etc.).
- boolean | AddEventListenerOptions — same as the standard
Removes a previously registered receive handler. The same target.type and
handler reference must be passed to identify which listener to remove.
pigeon.removeReceiveMessageListener({
type: "messageType",
}, receiveHandler);pigeon.addSendMessageListener<T>({
type: "messageType",
}, sendHandler);
// Match every message
pigeon.addSendMessageListener("*", sendHandler);-
target
"*"|{ type: string }|{ type: RegExp }- Specifies which messages to listen for.
"*"matches every message.options(e.g.once) work natively.{ type: string }matches messages whosetypeis exactly equal.optionswork natively.{ type: RegExp }matches messages whosetypematches the pattern. Note: with aRegExp, theoptionsargument is not accepted (the underlying listener does internal filtering, which would consume options likeonceon filtered-out messages). Use a stringtypeor"*"if you need EventAPI options.
-
handler
(message: SendMessage<T>) => voidA callback function that will be invoked when a matching message is sent.
- options
- boolean | AddEventListenerOptions — same as the standard
addEventListenerthird argument (once,signal, etc.).
- boolean | AddEventListenerOptions — same as the standard
Removes a previously registered send handler. The same target.type and
handler reference must be passed to identify which listener to remove.
pigeon.removeSendMessageListener({
type: "messageType",
}, sendHandler);Fires when the init handshake from the host completes (i.e. when pigeon.id
is assigned and pigeon.isConnected becomes true).
pigeon.addConnectListener(() => {
console.log("connected as", pigeon.id);
});Fires when the underlying WebSocket closes — cleanly, due to a network error
mid-stream, or because the initial connection attempt failed. The handler
receives a { code, reason, wasClean } object extracted from the WebSocket
CloseEvent.
pigeon.addDisconnectListener(({ code, reason, wasClean }) => {
console.log("disconnected", { code, reason, wasClean });
});options for both APIs accepts the standard addEventListener third argument
(once, signal, etc.).
Deliberately closes the underlying WebSocket and stops auto-reconnect, while
keeping the instance and its listeners intact. Use it for an app-initiated
disconnect (e.g. the user logged out) where registered listeners should still
observe the disconnect event.
pigeon.close();With autoReconnect enabled, an ordinary close — including a clean close
initiated by the peer — does not reconnect; only an unclean drop (network
error, etc.) does. close() additionally cancels any pending reconnect attempt,
so it is the way to stop the reconnect loop without discarding the instance.
Re-opens the connection after a close(), reusing the same options and the
already-registered listeners — the counterpart to close(). Use it to
reconnect to the same room without recreating the instance (e.g. the user logs
back in).
pigeon.close();
// ...later
pigeon.reopen();It is a no-op while a socket is still OPEN or CONNECTING, so calling it on a
live connection will not orphan the current socket. As with autoReconnect, a
fresh client id is assigned on rejoin unless staticId was specified; re-read it
from pigeon.id after reconnecting.
Closes the underlying WebSocket and unregisters every listener this instance has added. Call it when the Pigeon will not be used again (route change, component unmount, etc.) to release resources promptly.
pigeon.destroy();Note: listeners you register directly on window / globalThis (e.g.
window.addEventListener("pigeon:receive", ...)) are outside this instance's
bookkeeping and will not be removed by destroy() — call removeEventListener
yourself.
Automatically replies with a pong message when ping is receivec.
onReceiveMessage/onSendMessageare now deprecated. Replace them withaddReceiveMessageListener/addSendMessageListener. The deprecated methods are still available as aliases until v1.0.0.- Breaking:
addReceiveMessageListener/addSendMessageListener(and the deprecated aliases) no longer accept theoptionsargument whentarget.typeis aRegExp. Calling with both will fail to type-check, and at runtime theoptionsargument is silently ignored with aconsole.warn. Use astringtypeif you need EventAPI options. - The wildcard form moved from
{ type: "*" }to"*"as the first argument:// Before pigeon.addReceiveMessageListener({ type: "*" }, handler); // After pigeon.addReceiveMessageListener("*", handler);
{ type: "*" }still works in v0.3.0 with aconsole.warn, and will be removed in v1.0.0. The change disambiguates "match every message" from a future filter-object API where omitting a key means "no constraint on that key".
The following methods are kept as aliases for backward compatibility. New code
should prefer the add* / remove* variants.
onReceiveMessage(target, handler, options?)— alias ofaddReceiveMessageListener.onSendMessage(target, handler, options?)— alias ofaddSendMessageListener.
Listeners registered with the deprecated methods can still be removed via
removeReceiveMessageListener / removeSendMessageListener by passing the same
target.type and handler.