Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
en:comet:javascript_api [2017/08/15 16:40]
levhav
en:comet:javascript_api [2019/12/18 02:05] (current)
Line 1: Line 1:
 <​rst>​EN::​002-API::​001-JavaScript API::​001-JavaScript API</​rst>​ <​rst>​EN::​002-API::​001-JavaScript API::​001-JavaScript API</​rst>​
 +<​rst>​Header:​ JavaScript API</​rst>​
  
 ====== JavaScript API ====== ====== JavaScript API ======
Line 57: Line 58:
   * web_* - channels which can receive messages from CometQL and JS API.   * web_* - channels which can receive messages from CometQL and JS API.
   * track_* - automatic notification JS api about what someone has subscribed or unsubscribed from this channel   * track_* - automatic notification JS api about what someone has subscribed or unsubscribed from this channel
-====== The subscription on receiving private messages ( pipe msg )======+====== The subscription on receiving private messages ( pipe msg ) ======
 The subscription on messages from server, delivered in order to authorization data (by user’s id): The subscription on messages from server, delivered in order to authorization data (by user’s id):
 <code JavaScript>​cometApi.subscription("​msg",​ function(e){ console.log(e)})</​code>​ <code JavaScript>​cometApi.subscription("​msg",​ function(e){ console.log(e)})</​code>​
Line 63: Line 64:
 <code JavaScript>​cometApi.subscription("​msg.event_name",​ function(e){ console.log(e)})</​code>​ <code JavaScript>​cometApi.subscription("​msg.event_name",​ function(e){ console.log(e)})</​code>​
  
-<note tip>More details about authorization mechanism on comet server ​you and about private messages look in article “[[en:​comet:​authentication|User’s authorization on comet server]]”</​note>​+<note tip>More details about authorization mechanism on comet server and about private messages ​you can look in article “[[en:​comet:​authentication|User’s authorization on comet server]]”</​note>​
  
-====== Subscription on changing user’s status ( pipes user_status_* )======+====== Subscription on changing user’s status ( pipes user_status_* ) ======
  
 It is possible to subscribe from JS on notifications about some user’s activity on comet server. ​ It is possible to subscribe from JS on notifications about some user’s activity on comet server. ​
Line 106: Line 107:
 ====== Sending messages to channel from JS (pipes web_*) ====== ====== Sending messages to channel from JS (pipes web_*) ======
  
-The function web_pipe_send ​lets from JavaScript ​send messages ​to channel ​bypassing ​your server (directly calls comet server). It allows resending messages between clients without loading your server. Also owing to direct call to comet server, delivery time of message from client to client is minimal.+The function web_pipe_send ​allows sending messages ​from JavaScript to channel ​by passing ​your server (directly calls comet server). It allows resending messages between clients without loading your server. Alsoowing to direct call to comet server, delivery time of message from client to client is minimal.
  
-<note important>​Send messages to channel from JavaScript is able when name of channel begins with “web_”. CometQL does not have any limitations in this case.</​note>​+<note important>​Sending ​messages to channel from JavaScript is able when name of channel begins with “web_”. CometQL does not have any limitations in this case.</​note>​
  
 <code JavaScript>​cometApi.web_pipe_send("​web_pipe_name",​ "​event_name",​ "​message"​)</​code>​ <code JavaScript>​cometApi.web_pipe_send("​web_pipe_name",​ "​event_name",​ "​message"​)</​code>​
Line 115: Line 116:
 <code JavaScript>​cometApi.subscription("#​channel_name",​ function(e){ console.log(e)})</​code>​ <code JavaScript>​cometApi.subscription("#​channel_name",​ function(e){ console.log(e)})</​code>​
  
-Since comet server supports users’ authorization,​ it automatically adds user id to message such a way that a sender can’t send foreign id. To deactivate this option, is needed to add symbol “@” before channel name which would accept message. In this case, delivered message would be look alike that it was send by non-authorized user.+Since comet server supports users’ authorization,​ it automatically adds user id to message such a way that a sender can’t send foreign id. To deactivate this option, is needed to add symbol “@” before channel name which would accept message. In this case, delivered message would be look alike that it was sent by non-authorized user.
 <code JavaScript>​cometApi.web_pipe_send("​@web_pipe_name",​ "​event_name",​ "​message"​)</​code>​ <code JavaScript>​cometApi.web_pipe_send("​@web_pipe_name",​ "​event_name",​ "​message"​)</​code>​
    
Line 132: Line 133:
 </​code>​ </​code>​
  
-Field server_info.user_id in incoming message fills out by some information besides zero just if message was sent to channel from JavaScript API and sender was authorized on comet server. Also it contains sender’s user_id.+Field server_info.user_id in incoming message fills out by some information besides zero just if message was sent to channel from JavaScript API and sender was authorized on comet server. Alsoit contains sender’s user_id.
 ====== Getting the last messages from channel ====== ====== Getting the last messages from channel ======
-The comet server includes possibility to activate ​storing the last N messages mechanism for some channels.+The comet server includes possibility to activated ​storing the last N messages mechanism for some channels.
 <note tip>​Logging of transferred messages may be activate by CometQL query to table “[[en:​comet:​cometql|pipes_settings]]”.</​note> ​ <note tip>​Logging of transferred messages may be activate by CometQL query to table “[[en:​comet:​cometql|pipes_settings]]”.</​note> ​
  
Line 143: Line 144:
 ======= Getting a number of subscribers ======= ======= Getting a number of subscribers =======
  
-The function “count_user_in_pipe” can helps to determine a number of subscribes for current channel.+The function “count_user_in_pipe” can help to determine a number of subscribes for current channel.
  
-Also this function has the first argument – name of channel and the second is callback function which holds an answer.+Alsothis function has the first argument – name of channel and the second is callback function which holds an answer.
 <code Javascript>​ <code Javascript>​
 cometApi.count_users_in_pipe("​web_chat_pipe",​ function(res) cometApi.count_users_in_pipe("​web_chat_pipe",​ function(res)
Line 153: Line 154:
 </​code>​ </​code>​
  
-Besides of [[en:​comet:​cometql|CometQL query]], this function can show a number of subscribers only for those channels which name begins ​with “web_” (for example, for “web_chat_pipe” it will work but for “chat_pipe” – will not work). This limitation entered in order to be able to create such a channel which a number of subscribers can’t be accessible for anyone via JS API.+Besides of [[en:​comet:​cometql|CometQL query]], this function can show a number of subscribers only for those channels which names begin with “web_” (for example, for “web_chat_pipe” it will work but for “chat_pipe” – will not work). This limitation entered in order to be able to create such a channel which a number of subscribers can’t be accessible for anyone via JS API.
    
 ======= Determination of authorization status on comet server ======= ======= Determination of authorization status on comet server =======
Line 175: Line 176:
 These functions will be called when authorization status will be changing. I.e. at least once an authorization will be successful or unsuccessful when connecting to comet server or while status will be changed. ​ These functions will be called when authorization status will be changing. I.e. at least once an authorization will be successful or unsuccessful when connecting to comet server or while status will be changed. ​
  
-Also you can call the function “isAuthorized” to determine authorization status.+You can also call the function “isAuthorized” to determine authorization status.
 <code JavaScript>​cometApi.isAuthorized()</​code>​ <code JavaScript>​cometApi.isAuthorized()</​code>​
 The function “isAuthorized” may return 3 different values: ​ The function “isAuthorized” may return 3 different values: ​
   * false – authorization is not complete;   * false – authorization is not complete;
-  * true authorization is complete;+  * true – authorization is complete;
   * undefined – the status in undefined – this answer returns even before an attempt to connect to comet server.   * undefined – the status in undefined – this answer returns even before an attempt to connect to comet server.