#Socket IO interactions with Express and Service/Controller

1 messages Β· Page 1 of 1 (latest)

mystic forge
#

Hello guys ! Here is an image of my "issue" ! I'm trying to find how I could link my GameService instance with the websocket, because I need to add methods on both sides ( GameService calling socketNamespace.emit and socketNamespace.on calling GameService methods ), and so, as I wanted to avoid circular imports ( I think we care about that right ? ), I'm currently trying to find a solution for this kind of use case, how could I resolve that and link my GameService to the socket IO namespace properly ?
Hope you guys can help ! That would be really amazing ! πŸ™

hoary sigil
#

I suggest using rxjs :)

mystic forge
mystic forge
#

And how could that be usefull in this case ?

hoary sigil
mystic forge
hoary sigil
#

Every incoming connection can be an observable that you can push to a subject, all while consuming ends can use filter, map etc to get their required state data

mystic forge
#

Kk it seems a bit complicated but I'll have a look and try to understand how it works and how I can implement this in my project

2 other questions :

If I want to do that without using Event Emitter / RxJS, IS there any other way to do it than passing the ws to the service constructor ? ( Or in the other way, create the socket events / namespace inside of my constructor so I can access both socket and service

And last one, is that a good thing to use functions to create my websockets + his events / namespaces, for exemple, adding to my main file :

const io = createWebSocket(server)

And in createWebSocket I do like

const io = new Server(server)

io.on('connected', (socket) => {
registerEvents(io, socket)
})

return io
hoary sigil
mystic forge
#
  1. Kk so it is way better having some sort of event emitter to manage interactions between websocket and controllers / services ?

  2. Then, as my websocket will be defined with this, my service will not be called anywhere, so, should I create a files importing all my services to the main file / process to init these ?

mystic forge
#

Or where / how should I import / init my services if they are not being used by the routes ? ( And so they are not imported to my app)

mystic forge
#

πŸ‘€

mystic forge
#

Idk if there's a better way to do that or...

hoary sigil
#

It's hard to respond to number 2 without knowing what your GameService does

mystic forge
#

It is basically a service that is listening events from a game client ( League of Legends here ), and fetching data every Xs, then formatting / treating the data to send it to the websocket / emitter so it can be sent to the client to update the opened pages

#

Here is the code I have rn, I just need to add the emit / send data to the emitter / websocket, and add connection to the game client socket to listen to events

#

But this is basically the code I have rn

#

This is the code of my service rn, and I might have a few like this one

#

And I don't really know where / how I should init these services in my project, to create the instance of the service and so make it enable / available

hoary sigil
mystic forge
hoary sigil
mystic forge
#

So add the

new LeagueService()

In the main index.ts ?
But what if I have a lot of services to init ? ( 8/10 )
That's why I was thinking about adding those inside a specific index file just for services

mystic forge
#

But basically that's the same thing
I need to import my services like this and just run new Service() in my index file right ?

mystic forge
#

πŸ‘€

hoary sigil
#

The way I prefer to do it is this in a more traditional codebade:

  • /some-feature/service.ts
export const SomeService = (deps: Deps) => ({})
  • /other-feature/service.ts
    // similar

  • /services.ts

export function getSomeService () {
  return isProduction()
    ? SomeService({ whatever, deps })
    : DevelopmentSomeService({ wevs })
}

/* etc*/
  • /index.ts
function main () {
  const someService = getSomeService()

  const someDependentComponent = SomeDependentComponent({ someDependentComponent })
}

main()
mystic forge
#

kk, would you wrap the service in a function rather than in a class ?

#

And so, if I look at what you sait, should I pass the Event Emitter / RxJS observable inside the deps or is it fine importing it directly in service.ts ?

#

πŸ˜„

#

Also last question, could I do a global function to start my services like
services.ts

export function runServices() {
  const service = getSomeService()
  const service2 = = getOtherService()
  // etc ...
}

And just call this in my main.ts if I don't need my services inside of it as variables ?

mystic forge
#

idk if its fine doing like that or no

hoary sigil
hoary sigil
mystic forge
#

Thx a lot for your help

mystic forge
#

So, I was finally able to make it works, but now I have one issue
My service is running, and it has a variable / property service.status ( active, starting, inactive )
When the client connect to the service, I would like the client to be able to retrieve the current status ( then, after that, the client will receive status updates from websocket )
How could I manage to give the user the first status when he open my page ( react app ) ? Should I use an http request or socket message may be ?

hoary sigil
#

I would do it all over the socket.

mystic forge
#

Idk the name exactly but should be that

mystic forge
hoary sigil
#

Otherwise a regular Subject is good too.

#

If something produces values only it's a Observable, to which you can subscribe a Subject