node.js with a statemachine

I understand that node.js is a single thread and is a single process in the memory, I’m working on a project employing state machine and wondering how that will work in this context. I’m feeling the states will be shared across users because it’s a single thread or a single process. Calling out for directions/advices.

let’s say I’ve state A, state B, state C

Application can only transition in this sequence, A -> B -> C

Initial state is A,

user 1 requests and as a result the state machine moves to state B and then finally state C

user 2 requests, will the state machine for this new user be in state A or state C?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

This article has a very interesting approach on how to deal with the FSM abstraction under an event based environment.

http://www.robert-drummond.com/2015/04/21/event-driven-programming-finite-state-machines-and-nodejs/

See also:

Solution 2

If you know about Stateless C# and experience, you can use Stateless npm porting for TypeScript/JavaScript: https://www.npmjs.com/package/stateless

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply