0

I have a controller corresponding to POST: /chat/{id}

I can make the controller in many ways and I do not know which to go for for better scalability and performance

1: Dispatch a Job to Queue

public function store(ChatMessageRequest $request, $chatId) {
    ChatMessageJob::dispatch($chatId, auth()->user()->id, $request->message);
    return response()->json(null, 200);
}

2: Create and Broadcast (the Job and Queue)

public function store(ChatMessageRequest $request, $chatId) {
    $message = ChatMessage::create(['chat_id' => $chatId, 'user_id' => auth()->user()->id, 'message' => $request->message]);
    broadcast(new ChatMessageSent($message));
    return response()->json(null, 200);
}

I do not know which is better for my need so is Dispatching a Job for messages needed?

I mostly want to get the response of 200 to the client after the job is finished in the Queue but I don't know how to track that in case database failed to create

tldr: do I go for

Dispatch Job -> Create Record -> Broadcast Record

or

Dispatch Job -> Broadcast Record -> Create Record

or

Create Record -> Broadcast Record

or

Broadcast Record -> Create Record

to have scalability and performance with data consistency?

0 Answers0