Suitable for MMO server?

561 views
Skip to first unread message

Federico Mouse

unread,
Nov 11, 2016, 1:23:36 AM11/11/16
to vert.x
I have a multiplayer game with a websocket server currently made with Node.js, and I want to rewrite it in Java because I need multithreading for performance.

I like Vert.x for its ease of use, but as I've read, each event will not be executed concurrently. This makes me afraid because one server might have 200 concurrent users all sending data to the server many times per second (more or less 10 times per second), that means 2000 messages per second, and given that the message handler will only be executed secuentially, I cannot enjoy the benefits of multiple cores.

Am I right? Should I look for another websocket solution? Or does Vert.x offer another solution?

Thanks in advance.

Federico Mouse

unread,
Nov 11, 2016, 1:27:48 AM11/11/16
to vert.x
And a last question: is ServerWebSocket.writeBinaryMessage asyncronous? (does it run in another thread?)

javadevmtl

unread,
Nov 11, 2016, 9:51:48 AM11/11/16
to vert.x
Concurrent yes.

So long you deploy enough verticles.

DeploymentOptions options = new DeploymentOptions().setInstances(NUMBER OF INSTANCES HERE).setConfig(this.config());
vertx.deployVerticle("com.xxx.YourWebSocketVerticle", options);

But if you need sequential and you need to funnel everything through one "processor" then no amount of technology will help you. You need to find ways to allow multiple verticle work on your data and do some sort of sequential message processing.

Federico Mouse

unread,
Nov 11, 2016, 10:29:32 AM11/11/16
to vert.x
Oh, so I have to think of it in another way. I started developing it thinking of each Verticle as a game server (with a websocketserver), but as you say, I should centralize the game server and convert every verticle in just a "router" between the users and the game server? Is this a typical usage of Vert.x?

I've made a game server before with Grizzly websocket server and It was different (it had many worker threads for dealing with websocket I/O).

Julien Viet

unread,
Nov 11, 2016, 10:38:18 AM11/11/16
to ve...@googlegroups.com
Hi,

Vert.x can easily handle 2000 messages per second and much more, even on a single thread (well it depends what you do with the event :-) )

If you need to scale it to multiple cores, then you start your Verticle with a number of instances > 1, each core will handle one websocket in Round Robin fashion.

so I think your use case should be pretty much easily covered.

Overall I think you need to understand that “concurrent != parallel”, Vert.x is single threaded (by default as said above) but a single thread is able to handle lot of concurrency and you should read the Vert.x documentation.

Julien

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/dfb6d669-4c05-4058-836e-6347f9ac9780%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Federico Mouse

unread,
Nov 11, 2016, 10:52:18 AM11/11/16
to vert.x

If you need to scale it to multiple cores, then you start your Verticle with a number of instances > 1, each core will handle one websocket in Round Robin fashion.


If I do that, I should start a WebSocketServer listening to the same port in each verticle instance, right?
And where should I put the Game Loop? In the central process that creates all the verticle instances? 

Julien Viet

unread,
Nov 11, 2016, 1:08:12 PM11/11/16
to ve...@googlegroups.com
Hi,

your business logic can be in the verticle (that also start the websocket server)

Vert.x manages the loop, all you need is to feed it with events which can be:

- websocket frame events
- timer event (periodic task)
- your events (reacting to other events cited before)

do you have an example of a simplistic game in mind we can try to map ?



Jochen Mader

unread,
Nov 11, 2016, 1:22:43 PM11/11/16
to ve...@googlegroups.com
I'd recommend to keep the IO-logic (Socket-handling) and your business/game logic in separate verticles to scale them individually.


Hi,

To unsubscribe from this group and stop receiving emails from it, send an email to vertx+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Jochen Mader | Lead IT Consultant

codecentric AG | Elsenheimerstr. 55a | 80687 München | Deutschland
tel: +49 89 215486633 | fax: +49 89 215486699 | mobil: +49 152 51862390
www.codecentric.de | blog.codecentric.de | www.meettheexperts.de | www.more4fi.de

Sitz der Gesellschaft: Düsseldorf | HRB 63043 | Amtsgericht Düsseldorf
Vorstand: Michael Hochgürtel . Rainer Vehns
Aufsichtsrat: Patric Fedlmeier (Vorsitzender) . Klaus Jäger . Jürgen Schütz

Federico Mouse

unread,
Nov 11, 2016, 1:35:38 PM11/11/16
to vert.x


On Friday, November 11, 2016 at 3:22:43 PM UTC-3, Jochen Mader wrote:
I'd recommend to keep the IO-logic (Socket-handling) and your business/game logic in separate verticles to scale them individually.

That's the part that I've yet to understand, how to have the game loop in one verticle, the IO in another, and pass data between both.



On Friday, November 11, 2016 at 3:08:12 PM UTC-3, Julien Viet wrote:
Hi,

your business logic can be in the verticle (that also start the websocket server)

Vert.x manages the loop, all you need is to feed it with events which can be:

- websocket frame events
- timer event (periodic task)
- your events (reacting to other events cited before)

do you have an example of a simplistic game in mind we can try to map ?

The basics of the game is like this:

  • Game server constantly receives "moveTargetXY" packets which I store in the Players object
  • The game loop is executed X times per second, evaluating the moveTarget property of each Player, and moving the player to that position.
  • The game loop also sends the updated Players information to the connected clients.

The game loop is sequential, but some operations it does are done in threads so they can be executed concurrently (for example, making the World info packets and sending them to each player).


Until now what I have done is:
  • A verticle which starts a WebSocketServer, and also starts a game loop which runs in a separate thread.
This way, the game logic will scale (as I'm using Java threads to execute them), but the IO won't, as the Verticle message handler is always executed in the same thread.

How do you think I should solve it? Maybe 2000 ingoing messages per second whose Handler only set a variable won't be a problem for a single thread?

Also, the writeBinnaryMessage operation runs in the same thread that the other handlers? Or it's async and multithreaded?

I really appreciate your help

Julien Viet

unread,
Nov 11, 2016, 2:25:24 PM11/11/16
to ve...@googlegroups.com
have you read the entire Vert.x core documentation ?

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.

Federico Mouse

unread,
Nov 11, 2016, 2:50:51 PM11/11/16
to vert.x
Not all but a big part. The question about writeBinaryMessage being async and multithreaded or not isn't answered there.

Jochen Mader

unread,
Nov 11, 2016, 4:19:35 PM11/11/16
to ve...@googlegroups.com
Every operation in Vert.x that isn't explicitly labeled differently is non-blocking.

To unsubscribe from this group and stop receiving emails from it, send an email to vertx+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Julien Viet

unread,
Nov 11, 2016, 5:58:34 PM11/11/16
to ve...@googlegroups.com
in vertx everything is asynchronous, non blocking and single threaded.



Julien Viet

unread,
Nov 11, 2016, 6:03:47 PM11/11/16
to ve...@googlegroups.com
On Nov 11, 2016, at 7:35 PM, Federico Mouse <federi...@gmail.com> wrote:



On Friday, November 11, 2016 at 3:22:43 PM UTC-3, Jochen Mader wrote:
I'd recommend to keep the IO-logic (Socket-handling) and your business/game logic in separate verticles to scale them individually.

That's the part that I've yet to understand, how to have the game loop in one verticle, the IO in another, and pass data between both.



On Friday, November 11, 2016 at 3:08:12 PM UTC-3, Julien Viet wrote:
Hi,

your business logic can be in the verticle (that also start the websocket server)

Vert.x manages the loop, all you need is to feed it with events which can be:

- websocket frame events
- timer event (periodic task)
- your events (reacting to other events cited before)

do you have an example of a simplistic game in mind we can try to map ?

The basics of the game is like this:


the verticle should start an http server with a websocket handler

when a websocket connects, put that websocket in a map with the player ID

  • Game server constantly receives "moveTargetXY" packets which I store in the Players object

when the websocket handler receives the message it updates the state (Players) 
  • The game loop is executed X times per second, evaluating the moveTarget property of each Player, and moving the player to that position.
set a periodic timer with vertx.setPeriodic(100, id -> {
   // Evaluate the state of all players and move the players
   // use the Map<PlayerID, Websocket> to send each player its new position
});

this is more or less how I would write it.
  • The game loop also sends the updated Players information to the connected clients.

The game loop is sequential, but some operations it does are done in threads so they can be executed concurrently (for example, making the World info packets and sending them to each player).


Until now what I have done is:
  • A verticle which starts a WebSocketServer, and also starts a game loop which runs in a separate thread.
This way, the game logic will scale (as I'm using Java threads to execute them), but the IO won't, as the Verticle message handler is always executed in the same thread.

How do you think I should solve it? Maybe 2000 ingoing messages per second whose Handler only set a variable won't be a problem for a single thread?

Also, the writeBinnaryMessage operation runs in the same thread that the other handlers? Or it's async and multithreaded?

I really appreciate your help

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.

Federico Mouse

unread,
Nov 11, 2016, 9:43:10 PM11/11/16
to vert.x
On Saturday, November 12, 2016 at 6:58:34 AM UTC+8, Julien Viet wrote:
in vertx everything is asynchronous, non blocking and single threaded.


But earlier today a write to a socket failed, and the exception came from a thread with a different name than the thread which handled the incoming messages, could it be possible or I must saw bad?

Btw, thanks for your other comment, I'm doing it similar as how you said.

Federico Mouse

unread,
Nov 11, 2016, 9:51:34 PM11/11/16
to vert.x
And a related question: Is it possible to call ws.writeBinnaryMessage from multiple threads (maybe concurrently). Is serverwebsocket thread safe?

Julien Viet

unread,
Nov 12, 2016, 2:44:12 AM11/12/16
to ve...@googlegroups.com
can you post some code and the stack trace ?



--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.

Mumuney Abdlquadri

unread,
Nov 12, 2016, 5:23:01 AM11/12/16
to ve...@googlegroups.com
"Until now what I have done is:
  • A verticle which starts a WebSocketServer, and also starts a game loop which runs in a separate thread."
"This way, the game logic will scale (as I'm using Java threads to execute them), but the IO won't, as the Verticle message handler is always executed in the same thread."

You most likely are getting the exception in the threads you start. Try to do without creating threads. You can move that work into dedicated verticles.

On Sat, Nov 12, 2016 at 8:44 AM, Julien Viet <jul...@julienviet.com> wrote:
can you post some code and the stack trace ?
On Nov 12, 2016, at 3:43 AM, Federico Mouse <federi...@gmail.com> wrote:

On Saturday, November 12, 2016 at 6:58:34 AM UTC+8, Julien Viet wrote:
in vertx everything is asynchronous, non blocking and single threaded.


But earlier today a write to a socket failed, and the exception came from a thread with a different name than the thread which handled the incoming messages, could it be possible or I must saw bad?

Btw, thanks for your other comment, I'm doing it similar as how you said.

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages