Poker Node Js Github

  1. Node.js, being a non-blocking event-driven model presented developers a possibility to build applications in real-time. With push technology and 2-way connections, many top companies use it, as we’ve shown in our best Node.js app examples here. You can approach ThinkMobiles, as the Node.js development company./sociallocker.
  2. We use optional third-party analytics cookies to understand how you use GitHub.com so we can build better products. You can always update your selection by clicking Cookie Preferences at the bottom of the page.
  3. Casino-server -:fire: An online poker game server powered by Redis, node.js and socket.io 1071 Redis is an open source, BSD licensed, advanced key-value cache and store.

Poker + resume (nodeJS). Contribute to g20150120/poker development by creating an account on GitHub.

When searching in Github for real-time poker planning application,there is already multiple repositories using websockets on Github:

  • Wizehive/Firepoker (Javascript)
  • WASdev/sample.planningpoker (JAVA)
  • WayneYe/ScrumPoker (Javascript)
  • dctse/ScrumPoker (Javascript)
  • simbo1905/sprint-planning (Scala)
  • qJake/planning-poker (C#)
  • richarcher/Hatjitsu (Javascript)
  • inovex/planning-poker (Javascript)

Written in Javascript (and nodejs in many cases), JAVA, Scala, C#…

The one written in PHP I can find on the first Github search pageuses ajax requests every seconds Toxantron/scrumonline:

But does it means that PHP is not able to use websockets and make a real-time application ?

I often hear that PHP is not good for websockets, that it’s better to use nodejs blablabla

In fact, if I want to build a real-time application,I don’t care about using PHP or nodejs, or using websockets or something like long-polling…

But if you are already in a PHP eco-system, or if you love PHP,you may want to build your real-time application using PHP.

I’m not saying that PHP is a secondary choice, “because I don’t have the choice”,but this article will proove that it is possible to builda real time application using websockets over PHP.

I also do it in a structured code, and not only a websocket-server.php fileyou can find in all the “PHP chat with websockets” tutorials on the web.

Poker Node Js Github

Let’s build a real-time application

I’ll do it with Sandstone.This library extends Silex, so if you know Silex,you know already the half part of this tuto (the RestApi part).

Sandstone also adds websockets support,and provide some abstraction tools to create websocket topics the more simple way,or the more “Silex way”.

Tutorial

What are the application needs

My need is to build a planning poker application, where we can start a poker session, let teammates join it, and let them vote.

So, in a first part, I will build a RestApi with resources:

  • /teams: create a team, get a list of existing teams, join a team
  • /users: create an user with a pseudo, make an user vote (1, 2, 3, 5, 8, …).

Then, I will build the second part of the application, the real time vote using websockets.

I mean by real time vote the fact by once an user votes,all others user interfaces will be updated instantly:the user send the vote information to the server,then the server broadcast the vote information to others users in the team(no Ajax request every seconds).

Technical stack

  • Server:
    • Sandstone:
      • RestApi: create/join rooms
      • Websocket server: be notified when someone join our team, vote, or team vote is finished to refresh the view in real-time.
  • Client:
    • Bootstrap 4 + jQuery + Js app which uses api

I’ll use Docker and docker-compose, so that you don’t have to install PHP, ZMQ and ZMQ PHP extension.

Note:The application I’ll build in this article is available on Github:alcalyn/poker-planning

Part I: Rest Api with Silex

If you know Silex, you already know this part.We use Silex to build a light RestApi which handle the /teams and /users resources.

Silex is a microframework which allows to mount a web application easily,with a light router, a light service container (Pimple)…

Install Sandstone

Let’s bootstrap a Silex application, but not from scratch.

I will use here the Sandstone edition.

Sandstone edition is a Silex skeleton with websockets, Doctrine, JMS Serializer, web profiler…

Using Docker, following the documentation:

Docker helps us to mount our application web server, websocket server, …so we don’t have to install all that stuff.

Let’s check that it’s well installed by going to the diagnostic page:http://0.0.0.0:8480/hello/world.html.

If there is still orange or red boxes,you may just need to chmod -R 777 var/* or chown -R USER:GROUP .

I also have an access to the Symfony web profiler here:http://0.0.0.0:8480/index-dev.php/_profiler/.

The Sandstone edition structure

The edition has in fact 2 stacks:

  • The RestApi stack (app/RestApiApplication),
  • The websocket stack (app/WebsocketApplication).
Github

These 2 stacks extend a common stack (app/Application).

By this way, we do not load RestApi controllers in the websocket server,and we do not load websocket topics in RestApi stack.

Application, the common stack, contains:

Poker Node Js Github
  • services
  • Doctrine mappings
  • serializer metadata

RestApiApplication, the RestApi stack, contains:

  • controllers
  • converters
  • events to forward

WebsocketApplication, the Websocket stack, contains:

  • websocket topics

Create database schema and entities with Doctrine

The basic scenario is:

The user enters his pseudo, then see the list of the teams.He selects a team, then see the users and can vote.

So let’s map User and Team entities with Doctrine, in yaml format:

src/App/Resources/doctrine/App.Entity.User.dcm.yml:

src/App/Resources/doctrine/App.Entity.Team.dcm.yml:

Note:I don’t want to generate php model classes manually,so using yaml mapping allows me to generate them automatically.

Let’s tell Doctrine to use our yaml files (replace the 'annotation' one):

src/App/HelloProvider.php:

Then auto generate entities, and update the database schema:

You should see the generated entities in src/App/Entity/,and the tables in your phpmyadmin interface http://0.0.0.0:8481/.

Create Rest controllers

We now want a lot of routes such as:

  • POST /users route when a new user comes and enter his pseudo,
  • GET /teams for the teams list,
  • PUT /teams/{team}/users/{user} when an user joins a team,
  • POST /users/{user}/vote to allow a player vote…

Team controller

I won’t expose all controllers, but just the POST /users/{user}/vote one as an example.

Note: The whole application is available on Github,so check on Github src/App/Controllerto see all others controllers.

Let’s implement the POST /users/{user}/vote route:

src/App/Controller/UserController.php

Tiny controllers, thanks to converters

I don’t want to do a ->getRepository('Team')->findById($id) in my controllers.

So I can use converters (see Silex converters).

For example, src/App/Converter/TeamConverter.php:

Then I magically get my instances in my controllers from arguments when I do:

where app.converter.team:convert and app.converter.user:convertare callbacks to the convert method in TeamConverter and UserConverter.

Sure, converters need to be registered first as a service in RestApi stack:

src/App/HelloRestApiProvider.php

Move some logic from controller to service

As you know, business logic should be in services.

The service app.poker_planning contains some logic relative to Poker Planning voting.

src/App/Service/PokerPlanning.php:

Let’s register the service in common stack:

In src/App/HelloProvider.php:

Check your API by retriving all teams: http://0.0.0.0:8480/index-dev.php/api/teams

I also provide a Postman collection, so that you can play pre-saved requests.The collection is available here: poker-planning-postman-collection.json.

Part II: Real-time stuff with websocket

Well, we now have a working RestApi where we can retrieve teams,and post users votes.

Now, when someone votes, I want to keep updated all web clientsand display that an user voted in real-time.

Poker Node Js Github Tutorial

But I don’t want to request my RestApi every second to check if state has changed.

Display user vote in real time

The need: I want the RestApi notifies me when an user votes and when all users votedso I can reveal votes as soon as it is available.

How we will achieve that ?

The logic is:

  1. Someone votes, then calls PUT /users/1/vote.
  2. The RestApi handles the request,
  3. persist to user vote,
  4. then sends the 204 response to the client which has sent the vote.

But I also want that the RestApi notifies all my teammates that I voted.

The websocket solution:

All users should connect to a websocket,which is a connection that stays openso that both client and server can use it to send messages.

Node

Then the RestApi will notify through this websocket that I voted.

Sandstone uses the WAMP protocol. So instead of connect to the websocketand listen all messages through an unique channel,we can create multiple channels, or “topics”, and subscribe to a topic.

Users will then receive messages from this topic only.

I want to create a topic for each team, teams/1, teams/2, …

And I will dispatch messages relative to the team on the team channel.

Then, every user who joins a team must also subscribes to the team channelin order to receive real-time notifications (someone joined my team, someone voted).

Creating the websocket topic

Once you understand the logic, let’s implement it with Sandstone.

A EoleSandstoneWebsocketTopic class is responsible of the logic behind a websocket topic.

It implements for example the method onSubscribe, called when an user subscribes to this topic.

Use case: Send a message to new subscribing users, for example last messages sent.

It also implements the onPublish method, when an user send a message to this topic.

Use case: For chats: broadcast back the message to every users who subscribed to this topic.

It also provides a broadcast method to broadcast a message to every subscribing users.

First create the topic class src/App/Topic/TeamTopic.php:

And register the topic in websocket stack, in src/App/HelloWebsocketProvider.php:

Web client in my team #1 can now subscribe to teams/1, but nothing will happens for now.

Forward vote event to websocket topic

I want my topic to listen to the UserEvent::VOTED,which should be dispatched from the RestApi controller when someone voted.

But this event is dispatched in the RestApi stack,whereas my topic is in the websocket stack,this is to different processus.

Poker Node Js Github Plugin

Sandstone allows to dispatch event from RestApi processus to the websocket processus by forwarding it.

So we will need to create an event class, in src/App/Event/UserEvent.php:

Then, disptach the event from the controller, in src/App/Controller/UserController.php:

Nothing new here, just dispatching an event using Symfony EventDispatcher.

Now, we want to listen this event from the Topic class.To make the event dispatch from RestApi processus to Websocket one,Sandstone allows us to forward it:

src/App/HelloRestApiProvider.php:

Poker Node Js Github Commands

Then magically listen it in the TeamTopic class:

By magically I mean that the event instance is serialized,sent to the websocket server thread through a Push server (ZeroMQ),then deserialized and redispatched in the websocket server application event dispatcher.

By this way you’re able to magically listen an event from another PHP thread.

Note:Don’t forget to restart the websocket serverwhen the source code changed with:

make restart_websocket_server

Poker Node Js Github Js

Connect to websocket server from your Javascript application

You’ll need to test this with a websocket Javascript client.

A JS library that can be used with Sandstone (WAMPv1/Websocket protocol)is AutobahnJS 0.8.

Note: Another one could be wamp1,which can be loaded with npm, but I don’t tested it yet.

To be short, it will be something like:

The full front and back application can be installed and tested here:

Poker Planning

Julien Maulny