Re: [nodejs] How To get session of java

234 views
Skip to first unread message

Ben Noordhuis

unread,
Dec 12, 2012, 3:06:49 AM12/12/12
to nod...@googlegroups.com
On Wed, Dec 12, 2012 at 8:39 AM, ayaz ali <khatri....@gmail.com> wrote:
> hello everyone i have made a nodejs chat app with express and io. i want to
> integrate it with existing java application which is mode on java spring.
> flow will be like that when user login the application i want to get the
> session of java in my nodejs to get user online.

Define 'session'? If you mean the server-side state that the
JSESSIONID cookie refers to, you'll have to devise some RPC protocol
between Spring and node.js for getting that state out of your servlet
container.

Angel Java Lopez

unread,
Dec 12, 2012, 3:51:51 AM12/12/12
to nod...@googlegroups.com
Hi people!

Ummmm.. do you need somevalue of session? or only the name of the user, to be used at the chat activitiy?

In any case, I would implement:

- Java (web server, I guess) sends a token to browser. In this context, token is a random key (a GUID, maybe). Java web server keeps a weak dictionary (or something more elaborated, with time expiration) that associate key with the data you want to keep track.

- Web browser send the token to node, in a Socket.IO message

- Node save the token in the client socket object, if you need it more than one time

- Node call a simplre REST service at Java web server, sending the receiving context. If you need more that one data piece, maybe you can send an additional parameter like "email", or "firstName", specifying what you want to get.

- Java web server query the weak dictionary using the token as key, retrieving the required data, and sending it in the response

- You can add security measures to keep the REST service only accesible from your Nodejs machine

- A weak dictionary prevents Java application to hold for ever the data

Another approach:

- Java server sends the token to the web browser
- Java server call a REST service AT NODE JS server, sending the token, and the data you will be use at chat flow (that is, in the case the data you need is known in advance, and you don't need to send data from Node.js to Java)
- Node.js save the info in a dictionary (a simple javascrip object)
- Web browser send the token to node.js/socket.io during chat activity
- Node.js retrieved the rest of the data using the dictionary
- You must provide something to prune the dictionary (every token/key could have a "limit life" of 10 minutes, or delete the key if it is not used after X minutes)

Using the token, no sensible information is send to the browser. Having a random token, prevents cheating from a client.

You could use session.getId() as the key

Instead of calling node.js from java server using a REST service (or something alike) you could try an RPC, like dnode https://github.com/substack/dnode

Angel "Java" Lopez
@ajlopez
gh:ajlopez

On Wed, Dec 12, 2012 at 4:39 AM, ayaz ali <khatri....@gmail.com> wrote:
hello everyone i have made a nodejs chat app with express and io. i want to integrate it with existing java application which is mode on java spring. 
flow will be like that when user login the application i want to get the session of java in my nodejs to get user online.
Thanks 

--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

ayaz ali

unread,
Dec 12, 2012, 4:00:05 AM12/12/12
to nod...@googlegroups.com
i need the complete session object which is created after login. it may contain any thing any solution please

greelgorke

unread,
Dec 12, 2012, 4:12:17 AM12/12/12
to nod...@googlegroups.com
it depends on how java session is implemented. is it persisted in a db? then you just have to read the session-id cookie in node and fetch it from db. if its in-memory stored in java, then you have to implement a rpc/rest interface in java, which profides the session data as json to your node application. if it's pure cookie/localstorage based, thans just read them.

ayaz ali

unread,
Dec 12, 2012, 4:13:40 AM12/12/12
to nod...@googlegroups.com
its an httpsession

Angel Java Lopez

unread,
Dec 12, 2012, 4:18:07 AM12/12/12
to nod...@googlegroups.com
Umm... then, you could serialize the session to JSON, if the objects are suitable for that serialization.

Distributed session using memcached uses that approach

I don't know the "state of art" in Java to JSON serialization. Some resources

ayaz ali

unread,
Dec 12, 2012, 4:21:34 AM12/12/12
to nod...@googlegroups.com
is it possibe to acess session from javascript? hop eits silly questuin :p bt just for confirmation

Ben Noordhuis

unread,
Dec 12, 2012, 6:33:54 AM12/12/12
to nod...@googlegroups.com
On Wed, Dec 12, 2012 at 10:21 AM, ayaz ali <khatri....@gmail.com> wrote:
> is it possibe to acess session from javascript? hop eits silly questuin :p
> bt just for confirmation

What exactly is unclear? It's been spelled out for you several times now.

ayaz ali

unread,
Dec 12, 2012, 8:23:30 AM12/12/12
to nod...@googlegroups.com

Angel Java Lopez

unread,
Dec 12, 2012, 8:41:18 AM12/12/12
to nod...@googlegroups.com
I'm not sure what is your use case.

If you need some value at client side/javascript, and you are using JavaServerPages (JSP), you could try (quick and dirty):

<script>
var myclientsidevalue = '<%= session.getValue("myserversidename") %>'; // I don't remember the exact notation, method call, etc..
</script>

there are other "better" solution (using expression language instead of <%= %>.

But really, I didn't grasp your use case, and real/final problem to solve. And below question is out of topic in this list, I guess.

On Wed, Dec 12, 2012 at 10:23 AM, ayaz ali <khatri....@gmail.com> wrote:
is it possibe to acess session from javascript?

--
Reply all
Reply to author
Forward
0 new messages