Creating WebSocket events

Creating WebSocket events is easy enough. All you have to do is to add the @Event() decorator to your controller methods and that’s it. Here is an example:

import { Controller, Event } from '@Typetron/Router'

@Controller()
class HomeController {

    @Event()
    home() {
        return 'Welcome to Typetron!'
    }
}

This will create a WebSocket event listener called home, based on the method name, that you can send from the client. Then, Typetron will send back the content returned from the controller.

Note You can easily listen to event using the Typetron WebSockets library. Check Using WebSockets on frontend on how to do so.

You can change the name of the event by just passing the new name in the @Event() decorator:

import { Controller, Event } from '@Typetron/Router'

@Controller()
class HomeController {

    @Event('main')
    home() {
        return 'Welcome to Typetron!'
    }
}

This will create the main event listener instead of home.

You can also group events using the @Controller() decorator:

import { Controller, Event } from '@Typetron/Router'

@Controller('users')
class UserController {

    @Event()
    list() {
        return 'List of users'
    }
}

This will create the users.list event listener. Every method under this controller will have the users. prefix.

Let’s also send and listen to these events on a browser client: Using WebSockets on frontend

Sending messages to other clients

In progress