ejabberd - Comments for "user_receive_packet hook and JID parameter" https://www.ejabberd.im/node/3259 en i guess my question was https://www.ejabberd.im/node/3259#comment-53710 <p>i guess my question was regarding the first parameter to the hook, which is documented as JID. don't know what it should denote but in some MUC related testing I did, it always has the same value as To parameter and "to" attribute. so it seems like JID==To==to.<br /> so was wondering why the JID parameter? is there any scenario when JID /= To.<br /> thanks for the quick reply as always :-)</p> <p>JJ</p> Fri, 31 Oct 2008 17:45:23 +0000 jjoshi comment 53710 at https://www.ejabberd.im Design decision: save CPU consumption https://www.ejabberd.im/node/3259#comment-53705 <p>I didn't write those lines of code, but I can imagine the reason.</p> <p>Let's imagine I will receive this stanza:</p> <div class="codeblock"><code>&lt;iq from=&#039;localhost&#039; type=&#039;result&#039; to=&#039;badlop@localhost/Tka&#039;&gt;<br />&nbsp; &lt;query xmlns=&#039;jabber:iq:last&#039; seconds=&#039;104&#039;/&gt;<br />&lt;/iq&gt;</code></div> <p>In that case, the hook user_receive_packet is run with those parameters:</p> <div class="codeblock"><code>JID: {jid,&quot;badlop&quot;,&quot;localhost&quot;,&quot;Tka&quot;,&quot;badlop&quot;,&quot;localhost&quot;,&quot;Tka&quot;}<br />From: {jid,[],&quot;localhost&quot;,[],[],&quot;localhost&quot;,[]}<br />To: {jid,&quot;badlop&quot;,&quot;localhost&quot;,&quot;Tka&quot;,&quot;badlop&quot;,&quot;localhost&quot;,&quot;Tka&quot;}<br />Packet: {xmlelement,&quot;iq&quot;,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [{&quot;from&quot;,&quot;localhost&quot;},<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {&quot;to&quot;,&quot;badlop@localhost/Tka&quot;},<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {&quot;type&quot;,&quot;result&quot;}],<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [{xmlelement,&quot;query&quot;,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [{&quot;xmlns&quot;,&quot;jabber:iq:last&quot;},<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {&quot;seconds&quot;,&quot;104&quot;}],<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; []}]}</code></div> <p>As you noticed, the To parameter is <strong>equivalent</strong> to the 'to' attribute in the stanza. Please note both values are <strong>not equal</strong>: the stanza contains a string, and the parameter contains the string already parsed and splitted in the building parts of the JID (username, server, resource...).</p> <p>If you only have the stanza and you want to perform some operation with the destination address, you would need to parse the stanza searching the 'to' attribute. And quite probably you would also need to parse the string and split in parts.</p> <p>If the string was already searched and processed before calling the hook, it can be passed inside the hook with minimal cost. And if you need that To, you can use it with minimal cost. Imagine your hook is called for EVERY single stanza that ANY user in your server receives. A small cost reduction here means a large cost reduction in overall, right?</p> <p>The only drawback is that the To parameter is passed to all hook handlers, even those that are not interested in it. So maybe memory consumption increases a little. As you can see, in this design decision the factor to reduce was CPU consumption instead of memory consumption.</p> Thu, 30 Oct 2008 00:48:42 +0000 mfoss comment 53705 at https://www.ejabberd.im