jWebSocket Forum

[Search] Search   [Recent Topics] Recent Topics   [Hottest Topics] Hottest Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
About client ID which is connector ID and user login name map issue  XML
Forum Index » Getting Started
Author Message
Jerry

jWebSocket Forum Fan

Joined: 05/08/2011 09:49:30
Messages: 59
Offline

hi,
I am using Jwebsocket to develop one IM application, however I meet some design issue and need your help and comment. the case as below.

You know in IM chat application each user has login name, and related contacts , and I design this model, and when I start to use Jwebsocket and wrote my own plugin , I met one issue, you know in the SystemPlugIn , client ID stands for one user, however in the client side is user login name, not client ID. for example I send one message to another user, normally I set target user login name not client ID. because user login name makes more sense for user and when I login on in the two different place, one is mobile phone, another is browser in the computer .

and my contact send message to me, which client will he send to , mobile or computer, So in this case, it will be complex, however if we use the login name, we can find out the two client ID and send it to two clients .

another case, when my contact is offline, its client id is null, and I have to store the message into the DB and when target user is login ,I will notify this user. In all I have to user login name , not client ID .

So to implement this , I have to implement one map between user login name and multi connector client ID and when connector stop , I have to update the map and make the client ID as null .

So here I want your comment whether it is good way to do it
vbarzana

jWebSocket Forum Fan
[Avatar]

Joined: 11/07/2011 09:13:42
Messages: 109
Offline

Hi Jerry,
Very good question! But this is not a jWebSocket issue, ok, let me try to explain it to you.

jWebSocket engines handle the connections with the clients using the so called Connectors, these connectors as you said have an ID, but of course, they also can have variables, you can access to them with the method getVar() and setVar(), jWebSocket has a logon mechanism that put a user inside the connector if the login credentials are correct, you can handle this mechanism, but is already done, I tried to explain a little bit here:
http://enapso.org/jwsForum219/posts/list/10046.page#16759

You can also see fundamentals demo http://jwebsocket.org/demos/fundamentals/fundamentals.htm there you will find an auth function that authenticates you through Spring Authentication, it will be good for you to have some knowledge about this type of authentication, so, I think that having a username variable in the connector (as it is already implemented in all jWebSocket demos), it doesn't matter where do you connect from, if you logon with jWebSocket server, you will receive a username.

Inside a plugIn or in any other part of the application where you have access to a connector you can ask for who is logged in each connector, here you have a little example that I prepared for you, this could be part of a chat application, just see the way I use the method connector.getUsername():


Best regards:
Victor

This message was edited 1 time. Last update was at 12/04/2012 16:01:41

[WWW]
Jerry

jWebSocket Forum Fan

Joined: 05/08/2011 09:49:30
Messages: 59
Offline

Hi, Victor,

Thanks so much for your so detail explanation ,in fact I already one plugin according to systemplugin implementation .

Here I have two more comments about connector ID and user name map and session issue .if you look at the code which you wrote below

// search if the user you want to send the message is online
Collection<WebSocketConnector> lConnectors = getServer().getAllConnectors().values();
for (WebSocketConnector lConnector : lConnectors) {
if(lConnector.getUsername().equals(lTo)){
lDestiny = lConnector;
break;
}
}

Here you need go through all the connectors, however if we 1M user online, it will become performance problems . So I need setup one map for username and connector ID ,when you user login in , I will set this user is online, if logout or client disconnect , we need remove the user in the cache map . So if in your server part there is one method or cache mechanism to allow us to cache the map is much better . of course we can implement ourselves .

So we have this kind of map between username and connectorID, when user send the message to target user, we just get the connector ID according to username and it need not go through all connectors .

second question, you know IM application, we need keep the user always online, however in the server Jwebscoket has session manager , if timeout, connector will be stopped ,the connection between client and server will disconnect . so my question, how to set the session will not be timeout ?


Thanks a lot and best regards
Jerry





 Filename USystemPlugIn.java [Disk] Download
 Description sample
 Filesize 32 Kbytes
 Downloaded:  1393 time(s)

Jerry

jWebSocket Forum Fan

Joined: 05/08/2011 09:49:30
Messages: 59
Offline

Hi, Victor,

Do you have any update ?

BTW: I am developing android client based on the demo , however you know in the mobile network is not so stable compare to fixed network .
the connect could be connect and disconnect . So I need handle the connection management . So could you pls give me some advice using jwebscoket client to handle the mobile network case . my requirement is when mobile network disconnect , I need try 3 times and each time is 5 second . after 3 times try if still failure ,I need show network is disconnect .and I need send the ping token to server to avoid the server session time out every 1 minutes . Do you think it is good way to do the heartbeat . can you advice me ?



Thanks
Jerry
vbarzana

jWebSocket Forum Fan
[Avatar]

Joined: 11/07/2011 09:13:42
Messages: 109
Offline

Hi Mr. Jerry,
You are totally right, going through all connectors to just ask for their username is very weight for the server and of course you would loose performance, I just simply tried to show you an example, I agree with you that you need a map structure to save the user name and ClientID. Your code is very good, so far as I can see you are a very productive user for jWebSocket .

Currently we are implementing many types of storages, but you can use already any type that you want from the existing ones, see the following examples:

MemoryStorage from org.jwebsocket.storage.memory.MemoryStorage, see an example with this storage:



MongoDBStorageV1 && MongoDBStorageV2 from org.jwebsocket.storage.mongodb.MongoDBStorageV1 / V2, see an example with this storage:



You can also try with MemcachedStorage. You can find it here org.jwebsocket.storage.memcached.MemcachedStorage, I didn't make an example, is almost the same of the others

And of course, you can make your own storage if you want

Then you need to register and unregister the users to the list, you also use the map for this, this is just as a hint, of course that you know how to do it, but probably someone else wants to know how to do this, however, here is a little example.


I hope I could give you a hand in this point, I will keep you informed when the new session mechanisms and the new storages be finished.

Best regards,
Victor

This message was edited 2 times. Last update was at 18/04/2012 00:58:00

[WWW]
Jerry

jWebSocket Forum Fan

Joined: 05/08/2011 09:49:30
Messages: 59
Offline

Hi, Victor,

Thanks a lot for your reply .your example is really very helpful for me and understand more about jwebsocket framework . really many thanks .

Besides this , I have another two issues mentioned , first one is about client connection management .Right now I am implementing android client , you know mobile network normally is not stable , sometimes it disconnects and sometime it connects, so we have to handle the connection ,we call connection management . and here I try to explain my case as below:

1. client network problem, for example, client using WIFI network or GPRS network and when network from connected to disconnected for several time and when the network is avaible , we can detect this ,in this kind of case, we will start the re-connect and re-login and client will be assigned new client ID even the disconnection is very short , for example 5 seconds . that's my current implementation, however I am thinking about whether we can re-use previous session if the disconnection is very short, for example
within 10 seconds , Can you comment it ?

2. about session management in the server side , you know IM chat application we would like to keep user always online unless you log off the system . however I looked at the session manager, it looks like there is timeout value, when the session meet the timeout , the connector will be stopped . does it mean the connection between client and server will be dropped , right ?
and how can I keep the session will not be timeout except some exception . and what does 0 in the timeout value setting? if I set it into 1000, does it mean 1 seonds or 1000 seconds, what's the unit

3. to avoid the session timeout , need client send the ping-pong message to server every several seconds or minutes ,could you pls clarify it .

4. I post several questions in the forum, however somehow they did not show in the forum , maybe they are still in the waiting list .
,main issue is NIOTcpengine , when I send some big image file to server, server will throws some exception and stop the connector and restart the connector . Could you pls check my question in the forum pending list ?

I think I still have lots of question and looking forward to your reply .you are very working hard I know.

Thanks a lot in advance


Jerry
kyberneees

jWebSocket Committer
[Avatar]

Joined: 10/10/2010 23:13:03
Messages: 96
Offline

Hi Jerry, I will try to help you as I can.

1. client network problem, for example, client using WIFI network or GPRS network and when network from connected to disconnected for several time and when the network is avaible , we can detect this ,in this kind of case, we will start the re-connect and re-login and client will be assigned new client ID even the disconnection is very short , for example 5 seconds . that's my current implementation, however I am thinking about whether we can re-use previous session if the disconnection is very short, for example
within 10 seconds , Can you comment it ?

You are totally right here, you need a session management mechanism in jWebSocket to allow client re-connections.
Please read the following documentation link, recently updated (very fresh) http://enapso.org/documentation/reference-guide/session-handling
The session management mechanism was recently fixed and updated, please check our last nightly build in the download section.

2. about session management in the server side , you know IM chat application we would like to keep user always online unless you log off the system . however I looked at the session manager, it looks like there is timeout value, when the session meet the timeout , the connector will be stopped . does it mean the connection between client and server will be dropped , right ?
and how can I keep the session will not be timeout except some exception . and what does 0 in the timeout value setting? if I set it into 1000, does it mean 1 seonds or 1000 seconds, what's the unit



sessionExpirationTime indicates in seconds the session expiration timeout. By default the sessionExpirationTime is 1 minute (60 seconds), of course you can change it . Cero as value for this property means: DO NOT PERSIST THE SESSION

3. to avoid the session timeout , need client send the ping-pong message to server every several seconds or minutes ,could you pls clarify it .

The PING-PONG mechanism is in the client low-level layer, should not be a developer responsibility. What client are you using?

4. I post several questions in the forum, however somehow they did not show in the forum , maybe they are still in the waiting list .
,main issue is NIOTcpengine , when I send some big image file to server, server will throws some exception and stop the connector and restart the connector . Could you pls check my question in the forum pending list ?

The NioTcpEngine is not ready for production already, we will fixed ASAP. However the TCPEngine has been tested and proved successfully, in Linux for example the engine can scale to thousands of clients "beautifully"


I hope you can be a bit better now, please don;t hesitate to contact us again.

Best Regards,
Rolando

Ing. Rolando Santamaría Masó
Email: rsantamaria@jwebsocket.org
Twitter: https://twitter.com/kyberneees
[WWW]
Jerry

jWebSocket Forum Fan

Joined: 05/08/2011 09:49:30
Messages: 59
Offline

Hi,Rolando

Thanks so much for your detail explanation . it is very helpful for me. however I still have some several questions .

Right now our test and dev environment as below :

1. I am using android client to standalone server which was located in the bin directory not embedded in Jetty or Tomcat,in the future I will move to web container . main purpose is to debug the code much ealier , and my environment is windows because I am in the dev mode. in the future I will deploy to Linux environment .

2. To session management mechanism . I looked at your article which was posted in the http://enapso.org/documentation/reference-guide/session-handling. it is
very good and I can understand it. however I cannot find example to implement the follow idea from android client to server plugin .

****************************************************************
The WebSocket protocol is built on top of the HTTP protocol, but a WebSocket connection has only one chance to interchange cookies between the client and the server (opening handshake). The jWebSocket engines make usage of the opening handshake to interchange the session identifier in a cookie named “SID”.
During the opening handshake the server expects to receive the SID cookie from the client to reestablish it stored session, however if the SID cookie is not present, the server generates one as follows:
Set-Cookie: SID=1e8f73b0704aa245ae8464e41c6cea49; HttpOnly

If jWebSocket server runs embedded in a servlet container, the session identifier is obtained from the HttpSession object present in the HttpServletRequest.

******************************************************************

If you can provide the whole example from client to server , especially in the android client to server part. for example. when the network disconnect for several seconds and then the network is available . and android need re-connect to server with existed SIT(session ID) and server find out this session ID is still valid in the server side , server will not send the new session ID to client and need not re-auth user . just connected and all message can be sent by server and client .

Right now I can not find the example in your android demo application and plugin part in the system part . So could you pls provide the example code to handle this re-connection case to help us to use in our application very quickly .


2.You said that The PING-PONG mechanism is in the client low-level layer, should not be a developer responsibility. What client are you using?

I am using android client . not browser . could you pls in the android case, how to use it and keep the session not timeout .


3. to engine part, As you said that your NIOTCPEngine is not ready for production yet. and your TCPengine is ready for thousands of client , it is good, however As our requirement , we need one server support to 10000 -20000 online clients ,So TCPengine is not good enough to support . however as I know NIOTCPengine or Mina or Netty should be possible . Am I right ? and As I can see you have Netty or Jetty\tomcat engine is in the code.
Could you pls let me which engine is ready for production besides TCPEngine ?


4. one suggestion from after I looked into your client source code and system plugin code .

Jwebsocket framework is very good framework , however I looked at the source code part BaseTokenClient.java

@Override
public void processPacket(WebSocketClientEvent aEvent, WebSocketPacket aPacket) {

Token lToken = packetToToken(aPacket);

String lType = lToken.getType();
String lReqType = lToken.getString("reqType");

if (lType != null) {
if (WELCOME.equals(lType)) {
fClientId = lToken.getString("sourceId");
fSessionId = lToken.getString("usid");
} else if (GOODBYE.equals(lType)) {
fUsername = null;
}
}
if (lReqType != null) {
if (LOGIN.equals(lReqType)) {
fUsername = lToken.getString("username");
mStatus = WebSocketStatus.AUTHENTICATED;
} else if (LOGOUT.equals(lReqType)) {
mStatus = WebSocketStatus.OPEN;
fUsername = null;
}

}


I think we should not do hard code for AUTHENTICATED . because as I know jwebsocket framework low layer should not depends on login token to support auth mechanism ,if we would like to change the LOGIN token into LOGON TOKEN , does it mean it will not AUTHENTICATED , right ?and you know in the system plugin part you already support two different token for authentication, one is login (old way) , another is Spring security by LOGON token . So if we need LOGON token,
does it mean that client will not be AUTHENTICATED ?

So my suggestion, framework should leave the authentication mechanism in the client and server slide to customer to implement , of course you can implement one example for our guideline .

Ok, Thanks so much your support , I believe this Jwebsocket server will become very good .

Best regards
Jerry



















kyberneees

jWebSocket Committer
[Avatar]

Joined: 10/10/2010 23:13:03
Messages: 96
Offline

Hi Jerry, back again to your interesting and very useful questions.

1. I am using android client to standalone server which was located in the bin directory not embedded in Jetty or Tomcat,in the future I will move to web container . main purpose is to debug the code much ealier , and my environment is windows because I am in the dev mode. in the future I will deploy to Linux environment .

I understand. However Linux can be used for developer purpose also, just a hint because I use Linux

2. To session management mechanism . I looked at your article which was posted in the http://enapso.org/documentation/reference-guide/session-handling. it is
very good and I can understand it. however I cannot find example to implement the follow idea from android client to server plugin .

Just to clarify, the session mechanism of jWebSocket is in a low-level layer, it is not a responsibility of a developer to deal with cookies or other handshake stuff. So up to this, you as a developer just need to know that
in the server side you can use the client (connector) session storage to persist data. This session data include authentication details, roles, whatever you need. Consider to read about authentication in jWebSocket using Spring: http://enapso.org/documentation/reference-guide/authentication.

> If you can provide the whole example from client to server , especially in the android client to server part. for example. when the network disconnect for several seconds and then the network is available . and android need > re-connect to server with existed SIT(session ID) and server find out this session ID is still valid in the server side , server will not send the new session ID to client and need not re-auth user . just connected and all
> message can be sent by server and client .

We don't need an example for this, because this is the normal behavior (expected). But you are right, the Java/Android client currently does not support cookies, so it does not support the session identifier dialog ;(, sorry.
Fix and update the Java/Android client is one of our priorities, we will fix it ASAP (as soon as possible). Currently I cannot give a date, but we are working hard on the project.
If you are able to help or support us in this field, you are welcome.

> Right now I can not find the example in your android demo application and plugin part in the system part . So could you pls provide the example code to handle this re-connection case to help us to use in our application very
> quickly .
When the Java/Android client be fixed, this will be solved. Currently the client just need to deal with cookies to support your requirements.

3. to engine part, As you said that your NIOTCPEngine is not ready for production yet. and your TCPengine is ready for thousands of client , it is good, however As our requirement , we need one server support to 10000 -20000 online clients ,So TCPengine is not good enough to support . however as I know NIOTCPengine or Mina or Netty should be possible . Am I right ? and As I can see you have Netty or Jetty\tomcat engine is in the code.
Could you pls let me which engine is ready for production besides TCPEngine ?

Give me some time to answer this interesting question with details.

4. one suggestion from after I looked into your client source code and system plugin code .
...

Agree with you. We will evaluate your suggestion and consider changes here.


------------------------------------------------------------------------------------------------------------------------
Concluding, your solution depends of the update in our Java/Android client wish will be solved ASAP.

Best Regards,
Rolando

Ing. Rolando Santamaría Masó
Email: rsantamaria@jwebsocket.org
Twitter: https://twitter.com/kyberneees
[WWW]
Jerry

jWebSocket Forum Fan

Joined: 05/08/2011 09:49:30
Messages: 59
Offline

Hi,Rolando

Thanks a lot for your detail explanation ,I am really looking forward to see your java client/android client to support cookie ready .
if you need me some support , for example, test even implement something, pls let me know .

BTW: I have another suggestion for your systemPlugin . I suggest you split this plugin function into two part, one is mandatory funtion, for example session management , client ID management and welcome token . this is fundamental function, remaining part . you can put into another new plugin for user to select whether use default authentication and authority , it is more flexible for us .

For example. I would like to use this server , however I would like to implement my own security mechanism , not depends on any other
framework (Spring security framework or others) .

What do you think my idea ?

Thanks
Jerry




kyberneees

jWebSocket Committer
[Avatar]

Joined: 10/10/2010 23:13:03
Messages: 96
Offline

Hi Jerry, as promised, the Java client already support cookies and is completely integrated with the server side session management.
So now, your android application will be allowed for a reconnection

The sources will be available for download in 1.0b7 version.

So we finally help you.

----------------------------------------

BTW: I have another suggestion for your systemPlugin . I suggest you split this plugin function into two part, one is mandatory funtion, for example session management , client ID management and welcome token . this is fundamental function, remaining part . you can put into another new plugin for user to select whether use default authentication and authority , it is more flexible for us .

For example. I would like to use this server , however I would like to implement my own security mechanism , not depends on any other
framework (Spring security framework or others) .

----------------------------------------

It is not necessary to split the SystemPlugIn, because is NOT mandatory for you as a developer to use the SystemPlugIn (Spring) authentication mechanism. Is as simple as create your own authentication plug-in and use it.
Currently we opened the java client internal "username" and "status" properties (1.0b7), to allow to developers to implement their own authentication mechanism.

Just a hint, before implement your own authentication mechanism, please take a look to the Spring security module documentation. You will appreciate the benefits.


Best Regards,
Rolando



Ing. Rolando Santamaría Masó
Email: rsantamaria@jwebsocket.org
Twitter: https://twitter.com/kyberneees
[WWW]
Jerry

jWebSocket Forum Fan

Joined: 05/08/2011 09:49:30
Messages: 59
Offline

Hi, Rolando

Thanks a lot for your quick reply and help, It is really good and I am looking forward to your Beta 7 release ,somehow I can not see Beta 7 release so far . original plan is yesterday .

Jerry
kyberneees

jWebSocket Committer
[Avatar]

Joined: 10/10/2010 23:13:03
Messages: 96
Offline

one question :you know in the Beta 7 , you change the session management mechanism . and I can see in the systempluin connector_start method , there is no session ID assigned any more. could you explain a little more, why you need do this kind of change .what change you did .and I more prefer to implement my own auth mechanism in my chat application . so basically I will not use systemplugin by my own plugin to support logon and logoff , is it still work , right ?

and one more again, in your mind, which engine is more mature to deploy in the production environment to support high concurrent user ?

------------------------------------------------------------------------------------

Hi Jerry, you are right, we removed the connector session identifier handling to the engine level, because currently we exchange session ids using cookies during the opening handshake (engine level). Currently every browser wish support cookies must be able to reconnect in the jWebSocket server. Also the Java and C# client.

As a developer you may not be aware of session management, the jWebSocket server automatically reconnects clients and recover their session storage if the session has not been expired.
from every part of your code, you can access the session storage by calling:
aConnector.getSession().getStorage()...
All the entries you put here, will be persisted during the session time.

Authentication is described in: http://enapso.org/documentation/reference-guide/authentication

To create your own authentication system, just CREATE IT as a plugin and call it from the client instead of the jWebSocket proposed one. However if you detail me your authentication scenario, I am totally sure that I can suggest you a jWebSocket compliant one in order to re-use the existing approach.

Best Regards,
Rolando

Ing. Rolando Santamaría Masó
Email: rsantamaria@jwebsocket.org
Twitter: https://twitter.com/kyberneees
[WWW]
kyberneees

jWebSocket Committer
[Avatar]

Joined: 10/10/2010 23:13:03
Messages: 96
Offline

Hi Jerry, back to our pending topic: Engines

Current provided jWebSocket engines for 1.0 version:
-----------------------------------------------------------------
> TCPEngine: Based on thread per connection model, this engine is ultra-fast for applications with no more than 1000 - 1500 concurrent users. This engine is NOT scalable. Current state is READY.
> NioTcpEngine: Based on Java NIO model, this engine is also very fast and robust. Is supposed to handle unlimited number of connections with minimal impact on the server load. The number of threads used to process the client requests (workers) is configurable, more threads more fast, but this really depends of the server hardware. Current state is READY.
> TomcatEngine: Based on the Tomcat support for WebSockets, this engine inherit Tomcat benefits like concurrency and session management. Current state is READY.
> JettyEngine: Based on the Jetty support for WebSockets, this engine inherit Jetty benefits like concurrency and session management. Current state is READY.
> GrizzlyEngine: Based on the Grizzly support for WebSockets. Current state is READY.


Important: The engines will be ready for production only AFTER the 1.0 final version release of jWebSocket.

PD: The next beta release of jWebSocket will include the NioTcpEngine updates and fixes. I hope you can enjoy it and also test it

Ing. Rolando Santamaría Masó
Email: rsantamaria@jwebsocket.org
Twitter: https://twitter.com/kyberneees
[WWW]
Jerry

jWebSocket Forum Fan

Joined: 05/08/2011 09:49:30
Messages: 59
Offline

Hi,

Many thanks for your quick reply .

I am looking forward to your new version ready .

Jerry
 
Forum Index » Getting Started
Go to:   
Powered by JForum 2.1.9 © JForum Team