How would I send a message to another client websocket

1,046 views
Skip to first unread message

thomas marchi

unread,
Nov 6, 2012, 7:48:22 AM11/6/12
to ve...@googlegroups.com
//Below is serverside javascript
//I can get messages back while on the same client but im having trouble understanding how i would send data to another client  

load ('vertx.js');

vertx.createHttpServer (). websocketHandler (function (ws) {
  ws.dataHandler (function (buffer) { 
var address = buffer.toString ();
        address.WriteTextFrame (ids); //just want this to send to another client's websocket
});
}.) Listen (8888)

/ / The incoming string handler has the full name. Is there an ID i should use That will aid me in this situation. 
//I'm new to websockets and vert.x so any leads would be much appreciated.

K

unread,
Nov 6, 2012, 8:37:20 AM11/6/12
to ve...@googlegroups.com
I just let every client register to the event bus with "client.<client_id>"
If I wanted clients to talk to each other, I just would tell every client every client address.

thomas marchi

unread,
Nov 6, 2012, 10:28:15 AM11/6/12
to ve...@googlegroups.com
Thanks for the reply! :) 
Is there any docs on how to send a message to that specific client via serverside js?

Really appreciate the reply, I'm going back and forth trying to get G-wan to play with websockets and if I can get vert.x down it would be a life saver.

The situation I'm looking at is like this.

A connects to websocket
B connects to websocket 

A sends message to server which then sends message to B

Kinda like a server relay

Joern Bernhardt

unread,
Nov 6, 2012, 10:38:11 AM11/6/12
to ve...@googlegroups.com
On Tuesday, November 6, 2012 4:28:15 PM UTC+1, thomas marchi wrote:
Thanks for the reply! :) 
Is there any docs on how to send a message to that specific client via serverside js?

Really appreciate the reply, I'm going back and forth trying to get G-wan to play with websockets and if I can get vert.x down it would be a life saver.

The situation I'm looking at is like this.

A connects to websocket
B connects to websocket 

A sends message to server which then sends message to B

Kinda like a server relay

In our project every client had something like a "client.<client-id>" registered (the client-id is generated by the session manager). A sends a message to the server ("chat-server" or something like that) with { "message" : "hello", "to" : "B" }. Server checks session manager for name B, receives a session id and sends { "message" : "hello", "from" : "A" } to "client.<client-id-of-B>".
 

thomas marchi

unread,
Nov 6, 2012, 10:46:25 AM11/6/12
to ve...@googlegroups.com
Exactly what I'm looking to do. Is there any demo code that I can look at that simple sends a message to that ids websocket, that would be a awesome! :)

Thanks for the reply Joern.

Joern Bernhardt

unread,
Nov 6, 2012, 11:26:10 AM11/6/12
to ve...@googlegroups.com
In the chat module (not in the official repo), you may find some pointers: https://github.com/campudus/vertx-chat
The server side module is written in Scala and there are some examples in the Readme in Java, how to use it. The chat doesn't use the session manager however but gives out his own client-id (called "token" in there).

Be aware: The module is still for Vert.x 1.2.3, so the registerHandler / unregisterHandler might look a little bit different with the newer Vert.x version.

I'd like to create some helpful tutorials about Vert.x and update the modules to 1.3.0, but I got some time restrictions. Right now, there is only a tutorial on how to use the session manager modules at our blog at http://campudus.com/en/415/vert-x-session-manager-modules/ - there will be more, as soon as time allows...

HTH,
Joern

thomas marchi

unread,
Nov 6, 2012, 12:23:55 PM11/6/12
to ve...@googlegroups.com
Yes, awesome thanks a ton guys. :) I look forward to the tuts.
Reply all
Reply to author
Forward
0 new messages