murysock
v0.0.6
Published
Murysock is a framework to create your own client websocket applications that interact with servers using the Event-Message architecture with few decorators.
Downloads
6
Maintainers
Readme
Getting started into Murysock!
@WebSockClient()
@WebSockConnected()
@WebSockGateway([address])
@WebSockData()
@WebSockDisconnected()
@WebSockError()
@WebSockEvent([eventname])
@WebSockException()
@WebSockInjectClient()
Each event is automatically routed to its respective handler, as long as the message payload received via the websocket is in the following JSON format:
{
"event": "[event-name]",
"data": "[event-data]"
}
Installation
npm install murysock --save
How to use?
The first thing to do to create your gateway and interact with a server that supports event-driven websocket is to create a class and annotate it with @WebSockGateway
, then add at least one event method annotated by @WebSockEvent
. After create the class, you need call websockConnect
to start the lifecycle of your gateway.
In Murysock, the lifecycle of a WebSocket connection is regulated by 4 decorators, they are: @WebSockConnected
, @WebSockDisconnected
, @WebSockError
, @WebSockException
. Whenever the connection is successfully established, the method decorated with @WebSockConnected
is invoked, just as when the connection is terminated the method decorated with @WebSockDisconnected
is invoked. In Murysock there are 2 types of exceptions, those caused by network failures and those caused by application code failures. These are handled by methods decorated with @WebSockError
and @WebSockException
respectively.
To send a response in each event you can use the reply
function and specify a remote event name and the data to be sent. You can call the reply
function more than one time in an event.
For a more detailed overview see the example of integration below using a Gateway to receive events:
import {
reply,
websockConnect,
WebSocket,
WebSockClient,
WebSockConnected,
WebSockGateway,
WebSockData,
WebSockDisconnected,
WebSockError,
WebSockEvent,
WebSockException,
WebSockInjectClient
} from 'murysock';
@WebSockGateway('ws://localhost')
export class Gateway {
@WebSockInjectClient()
public clientSock?: WebSocket;
@WebSockEvent('message')
async onMessage(
@WebSockClient() client: WebSocket,
@WebSockData() data: any)
{
return reply('broadcast', { hello: 'world' });
}
@WebSockEvent('broadcast')
onBroadcast(
@WebSockData() data: any)
{
console.log('broadcast', data)
}
@WebSockConnected()
onConnect(evt: any)
{
console.log('connected')
}
@WebSockDisconnected()
onDisconnect(evt: any)
{
console.log('disconnected')
}
@WebSockError()
onError(error: any)
{
console.log('error')
}
@WebSockException()
onException(exception: Error)
{
console.log('exception');
}
}
websockConnect(Gateway);
Metadata
Muryllo Pimenta de Oliveira – [email protected]
Distributed under MIT license. See LICENSE
for more informations.
Contributing
- Create a fork (https://github.com/MurylloEx/murysock/fork)
- Create a feature branch (
git checkout -b feature/fooBar
) - Commit your changes (
git commit -am 'Add some fooBar'
) - Send a push of your commit (
git push origin feature/fooBar
) - Open a new Pull Request