Removing Listeners
Use .remove(type, listener) to unregister a specific listener.
This is useful for toggling behavior on and off at runtime — like an auto-responder,
a debug logger, or any feature you want to enable temporarily.
Basic Usage
Pass the same event type and function reference you used with .on():
import { connect } from 'itty-sockets'
const channel = connect('my-channel')
const logMessage = ({ message }) => console.log(message)
// register
channel.on('message', logMessage)
// unregister — must pass the same function reference
channel.remove('message', logMessage)Toggle Pattern
A common pattern is toggling a listener on and off. Define the handler as a named function,
then call .on() to enable and .remove() to disable:
const greet = ({ uid, alias }) => {
channel.send({ text: `Hey ${alias}!` }, uid)
}
// enable auto-greeting
channel.on('join', greet)
// disable it later
channel.remove('join', greet)See this in action in the Auto-Responder example.
Named Functions Required
You need a reference to the original function to remove it. Anonymous inline functions can't be removed because there's no way to reference them later:
// this won't work — no way to reference the function later
channel.on('message', ({ message }) => console.log(message))
channel.remove('message', ???)
// instead, keep a reference
const logger = ({ message }) => console.log(message)
channel.on('message', logger)
channel.remove('message', logger) // worksNext: Reconnecting — resilient connections with a single line of code.