You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import{Agent,JXT}from'stanza';// 1. Declare our new custom stanza extension typeexportinterfaceMyStanza{
type: string;
value: string;}// 2. Begin injecting our plugin's type information into StanzaJS.declaremodule'stanza'{// 3. Declare a new method for the StanzaJS agentexportinterfaceAgent{sendMyStanza(jid: string,data: string): void;}// 4. Declare our event types. (Event names are the fields in AgentEvents.)exportinterfaceAgentEvents{
mystanza: Message&{mystanza: MyStanza};}// 5. Stanza definitions MUST be placed in the Stanzas namespacenamespaceStanzas{// 6. Attach our new definition to Message stanzasexportinterfaceMessage{mystanza?: MyStanza;}}}// 7. Create a plugin functionexportdefaultfunction(client: Agent,stanzas: JXT.Registry){// 8. Create and register our custom `mystanza` stanza definitionstanzas.define({element: 'foo',fields: {type: JXT.attribute('type'),value: JXT.text()},namespace: 'http://example.com/p/foo',path: 'message.mystanza'});// 9. Add API to the StanzaJS agent for sending `mystanza` dataclient.sendMyStanza=(jid: string,data: string){returnclient.sendMessage({to: jid,mystanza: {type: 'bar',value: data}});};// 10. Listen for incoming `mystanza` data and emit our own eventclient.on('message',msg=>{if(msg.mystanza){client.emit('mystanza',msg);}});};