new Xpush(host, appId, eventHandler, autoInitFlag)
Constructor of Xpush
Parameters:
Name | Type | Argument | Description |
---|---|---|---|
host |
string | address of session server to connect |
|
appId |
string | application id |
|
eventHandler |
string |
<optional> |
a function for processing the session event |
autoInitFlag |
boolean |
<optional> |
flag for whether to automatically initialize |
Examples
// Create new Xpush Object var xpush = new Xpush( 'http://demo.stalk.io:8000', 'sample' );
// Create new Xpush Object with event Handler var xpush = new Xpush( 'http://demo.stalk.io:8000', 'sample', function (type, data){ console.log( " type : ", type ); console.log( " data : ", data ); });
Methods
-
<static> addUserToGroup(groupId, userIds, cb)
-
Add a group id to one or multiple users.
Parameters:
Name Type Argument Description groupId
string <optional>
userId
userIds
array users array to add
cb
callback callback function to be performed after add
Example
xpush.addUserToGroup( 'james', ['notdol','john'], function( err, result ){ console.log( result ); )};
-
<static> createChannel(users, channel, datas, cb)
-
Generates a new channel.
Parameters:
Name Type Argument Description users
string userId array to invite channel
channel
string <optional>
Channel Id
datas
Object <optional>
JSON for additional channel information
cb
callback callback function to be performed after generate
Examples
// create random channel without data xpush.createChannel(['james', 'notdol'], function(err, data){ console.log( 'create channel success : ', data); });
// create a channel without data xpush.createChannel(['james'], 'channel02', function(err,data){ console.log( 'create channel success : ', data); });
// create a channel with data xpush.createChannel(['james'], 'channel03', {'NM':'james'}, function(err,data){ console.log( 'create channel success : ', data); });
-
<static> createSimpleChannel(channel, userObj, cb)
-
Generate a new
CHANNEL_ONLY
channel.Parameters:
Name Type Argument Description channel
string Channel Id
userObj
Object <optional>
UserObject( U : userID, D : deviceId )
cb
callback callback function to be performed after generate
Examples
// create simple channel without userObject xpush.createSimpleChannel('channel01', function(){ console.log( 'create simple channel success' ); });
// create simple channel with userObject xpush.createSimpleChannel('channel02', {'U':'james','D':'WEB'}, function(){ console.log( 'create simple channel success' ); });
-
<static> disableDebug()
-
disable debugging
Example
// disable debug xpush.disableDebug();
-
<static> enableDebug()
-
enable debugging
Example
// enable debug xpush.enableDebug();
-
<static> exitChannel(channel, cb)
-
Exit from the channel
Parameters:
Name Type Description channel
string Channel Id
cb
callback callback function to be performed after exit
Example
xpush.exitChannel( 'channel03', function(err, result){ console.log( 'result : ', result); });
-
<static> getChannel(channel) → {Object}
-
Get the current channel information in xpush object.
Parameters:
Name Type Description channel
string Channel Id
Returns:
return Channel Object
- Type
- Object
Example
var channel01 = xpush.getChannel('channel01');
-
<static> getChannelData(channel, cb)
-
Query the channel information of the server.
Parameters:
Name Type Description channel
string Channel Id
cb
callback callback function to be performed after query
Example
xpush.getChannelData( channel, function(err,data){ console.log( 'retrieve channel success : ', data); });
-
<static> getChannels(cb)
-
Query the channel list from the server.
Parameters:
Name Type Description cb
callback callback function to be performed after query
Example
// Get channels xpush.getChannels(function(err,datas){ console.log( 'channels : ' + datas ); });
-
<static> getChannelsActive(data, cb)
-
The channel list where the user is connected to the current channel will be viewed in redis.
Parameters:
Name Type Description data
Object ( 'key': '' )
cb
callback callback function to be performed after retrieve
Example
// Retrieve channels that start with channel xpush.getChannelsActive( {'key':'channel*'}, function(results){ console.log( 'results : ', results ); });
-
<static> getFileUrl(channel, fileName) → {string}
-
Get the file url which is uploaded completely
Parameters:
Name Type Description channel
string Channel Id
fileName
string received return name after file uploading
Returns:
url where you can download the file
- Type
- string
Example
var url = xpush.getFileUrl( 'channel03', data.result.name )
-
<static> getGroupUsers(groupId, cb)
-
Query the user list on the server that contains the group.
Parameters:
Name Type Description groupId
string Find groupId
cb
callback callback function to be performed after query
Example
xpush.getGroupUsers( 'james', function( err, users ){ console.log( users ); )};
-
<static> getUnreadMessage(cb)
-
Query the unread server message. After inquiry, call the
message-received
API to delete the viewed message.Parameters:
Name Type Description cb
callback callback function to be performed after query
Example
xpush.getUnreadMessage( function(err, result){ console.log( result ); });
-
<static> getUserList(params, cb)
-
Query the user list from the server.
Parameters:
Name Type Argument Description params
Object <optional>
param for search user.
cb
function callback function to be performed after query
Example
xpush.getUserList( {'page':{'num':1,'size':10} },function(err, users){ console.log( users ); });
-
<static> isExistChannel(channel) → {boolean}
-
confirm whether channel is exist or not
Parameters:
Name Type Description channel
string Channel Id
Returns:
- Type
- boolean
Example
var isExist = xpush.isExistChannel('channel03');
-
<static> joinChannel(channel, param, cb)
-
Join to the channel.
Parameters:
Name Type Description channel
string Channel Id
param
Object JSON Data ( U, DT )
cb
callback callback function to be performed after join
Example
xpush.joinChannel( 'channel03', {'U':['notdol']}, function(result){ console.log( 'result : ', result); });
-
<static> login(userId, password, deviceId, mode, cb)
-
Login user using the userId and password.
Parameters:
Name Type Argument Default Description userId
string User Id
password
string Password
deviceId
string <optional>
WEB Device Id
mode
string <optional>
mode
cb
callback callback function to be performed after login
Examples
xpush.login( 'james', '1234', function(err,data){ console.log('register success : ', data); });
// login with deviceId xpush.login( 'james', '1234', 'android', function(err,data){ console.log('login success : ', data); });
-
<static> logout()
-
Disconnect the session socket and channel socket.
Example
// logout xpush.logout();
-
<static> off(event, function)
-
Removes the event and function in the event stack
Parameters:
Name Type Description event
string key
function
function Example
xpush.off( 'message', function(channel, name, data){ console.log( channel, name, data ); });
-
<static> off(event, function)
-
Remove all event on the event stack.
Parameters:
Name Type Description event
string key
function
function Example
xpush.clearEvent();
-
<static> on(event, function)
-
Register the event and function to the event stack. The specified function is event is called the event.
Parameters:
Name Type Description event
string key
function
function Example
xpush.on( 'message', function(channel, name, data){ console.log( channel, name, data ); });
-
<static> queryUser(_params, cb)
-
Query the user list from the server. Paging is possible.
Parameters:
Name Type Description _params
Object ( query, column )
cb
callback callback function to be performed after query
Example
var param = {query : {'DT.NM':'james'}, column: { U: 1, DT: 1, _id: 0 } }; xpush.queryUser( param, function( err, userArray, count ){ console.log( userArray ); });
-
<static> removeUserFromGroup(groupId, userId, cb)
-
Delete the user from the group.
Parameters:
Name Type Argument Description groupId
string <optional>
userId
userId
string user's ID to delete
cb
callback callback function to be performed after delete
Example
xpush.removeUserFromGroup( 'james', 'notdol', function( err, result ){ console.log( result ); )};
-
<static> send(channel, name, data)
-
Transmits the data.
Parameters:
Name Type Description channel
string Channel Id
name
string EventName
data
Object String or JSON object to Send
Example
xpush.send( 'ch01', 'message', {'MG':'Hello world'} );
-
<static> setSessionInfo(userId, deviceId, cb)
-
Set the userId and deviceId to current xpush object. only if the session socket is already connected
Parameters:
Name Type Argument Description userId
string User Id
deviceId
string <optional>
Device Id
cb
callback callback function to be performed after set
Examples
// Set session info xpush.setSessionInfo( 'james', function(){} );
// Set session info with deviceId xpush.setSessionInfo( 'james', 'WEB', function(){} );
-
<static> signup(userId, password, deviceId, cb)
-
Register user using the userId and password.
Parameters:
Name Type Argument Default Description userId
string User Id
password
string Password
deviceId
string <optional>
WEB Device Id
cb
callback callback function to be performed after register
Example
// Add new user xpush.signup( 'james', '1234', function(err,data){ console.log('register success : ' + data); });
-
<static> updateChannel(channel, query, cb)
-
Modify the channel information of the server.
Parameters:
Name Type Description channel
string Channel Id
query
Object mongo DB query form as a JSON
cb
callback callback function to be performed after modify // update channel
Example
xpush.updateChannel( 'channel02', { $set:{'DT':{'NM':'notdol1'}}}, function(err, result){ console.log( 'result : ', result ); });
-
<static> uploadFile(channel, fileUri, inputObj, fnPrg, fnCallback)
-
Upload files using the REST API because the mobile is not supported file dom object
Parameters:
Name Type Description channel
string Channel Id
fileUri
string fileUri to upload
inputObj
Object JSON Objec( 'type' : '', 'name' : Original File name )
fnPrg
function function to show the upload progress
fnCallback
callback callback function to be performed after upload
Example
xpush.uploadFile('channelId', 'content://media/external/images/media/636', {type : 'image', name:'image.png' }, function ( data ){ console.log( data ); }, function (data){ console.log( data.response ); });
-
<static> uploadStream(channel, inputObj, fnPrg, fnCallback)
-
Upload the file DOM object using the socket stream to
Parameters:
Name Type Description channel
string Channel Id
inputObj
Object JSON Objec( 'file' : file DOM Oject for upload, 'type' : '' )
fnPrg
function function to show the upload progress
fnCallback
callback callback function to be performed after upload
Example
var fileObj = document.getElementById('file'); xpush.uploadStream( 'channel03', { file: fileObj }, function(data, idx){ console.log( 'progress : ' + data ); }, function(data,idx){ console.log( 'upload result : ' + data ); });
-
<private, static> XPush#emit(event)
-
Call a function that is registered in the event stack. When unread message exists, message will be stacked without causing the function of that event soon because it is being initialized status.
Parameters:
Name Type Description event
string key
-
<static> XPush#getChannelAsync(channel, cb)
-
Bring the channel information asynchronously. If the channel information is not present in the object, it queries the channel information from the server.
Parameters:
Name Type Description channel
string Channel Id
cb
callback callback function to be performed after get
Example
xpush.getChannelAsync( 'channel03', function(err, result){ console.log( 'result : ', result); });