Multiplayer games - using UDP

Hello all, I am a PlayCanvas newcomer with an interest in creating multiplayer games. I have successfully worked through the Multiplayer tutorial and I am trying to broaden my understanding by creating my own prototype demo with a Node.js server back end.

While the tutorial implements for the client to server communication, I discovered that there is also a UDP module available through npm called “dgram”.

I’m wondering if there is a preferred means to extend the playcanvas engine via scripts in the editor with NPM modules so as to leverage that particular library. My assumption is that that’s what the minified “socket.js” file in the tutorial example is for - to create a Javascript wrapper around the module (via the io.connect() call) to enable the Network class to perform its business logic, correct?

If I wanted to implement the dgram module in my code, would I need to write a similar wrapper? Is there a reason I am missing NOT to do this. It strikes me that performance-wise, UDP is the way to go to update the game state on the server with minimal latency.

This module also looks promising:

With a little more digging into the matter, I think I have answered my own question:

While most npm modules can be “Browserfied” to work on the client-side browser, the caveat is that TCP is the defacto protocol for browser communication.

The only work-around I can think of would be to download the PlayCanvas project and package it as a desktop app via Electron, but this kind of undermines PlayCanvas’ appeal as a universally accessible experience that is playable within a web page without plugins or downloads of any kind.

That being said, I can imagine a system where the matchmaking is done within the browser and the game is downloaded separately to run on the desktop as a standalone app. This might allow one to eke out every last drop of performance in a multiplayer scenario and additionally allow for other features like going full-screen, saving state locally, etc.

Personally I use Webpack with Playcanvas (with some custom plugins) this allows me to use NPM modules that are pure javascript.

At the moment the only way to get UDP from a browser is via WebRTC that is available in Edge, Chrome, Android and coming in Safari 11. Given that UDP is potentially lossy and does not guarantee delivery order its use always requires care in mullti-player games, but it does help with things sent frequently like position updates.

I’ve built a multiplayer library based roughly off the principles of Tasharen Networking (TNET for Unity) - basically RPC calls and network object instantiation with personal ownership. If you use UDP you really need “Ack and resend” functionality on your server - unless it’s only two players. As packets that are vital might be missed from only one end point.


WebRTC description covering UDP semantics:

Browser support

Mike, seems like you’ve written lib for everything, huh?

Could you please describe your workflow with extending PlayCanvas?

Hey Mike

Well I keep a current copy of the Engine source code and try to it “vanilla” until either a) something doesn’t work or b) something seems sub optimal or c) some feature is missing.

Being able to do this has been a revelation for me, with Unity I’d decompile the source to see why things happened, but it was tricky. Very hard to patch after. So now I monkey patch anything that seems missing. My ideal is to have very black boxed code that is tested and put to bed, so yep, I build a library for everything!

So if I want to extend something I either trace into the Engine or I grab the relevant function, monkey patch it and fiddle with it. My confidence in this has grown and grown. It’s easy enough to do and takes all of the “mystery” away.

Multiplayer is my current endeavour. I liked TNET very much on Unity so I’m trying to replicate that to a larger extent. Since moving to this new ES6 model it is harder to get this stuff to be available to everyone. I guess I’ll have to come up with some kind of library model for it at some point, the problem is that I’ll have to parse and rewrite the actual entity attachable scripts to use the library rather than importing things.

Thanks for detailed responses!

So I see you don’t mind to share your code, do you have repository with your stuff?
It would be extremely useful for community, I guess.

Yeah I’ve always done this with Unity before. There is a big community doing that there, smaller here :slight_smile: But you don’t want to build webGL games in Unity for mobile (or for anything really).

I believe that the final assembled piece of software for a thing has value, I believe the constituent elements should be available to everyone (unless it’s some super special algorithm or something). With Unity this got me loads of bug fixes and extra useful edge cases - so I benefit too.