new PublicMessageRequest(message[, params][, targetRoom])

Creates a new PublicMessageRequest instance. The instance must be passed to the SmartFox.send() method for the request to be executed.

Using this request a public message is dispatched to all the users in the specified Room, including the message sender (this allows showing chat messages in the correct order in the application interface); the corresponding event is the publicMessage event. It is also possible to send an optional object together with the message: it can contain custom parameters useful to transmit, for example, additional informations related to the message, like the text font or color, or other formatting details.

In case the target Room is not specified, the message is sent in the last Room joined by the sender.

NOTE: the publicMessage event is dispatched if the Room is configured to allow public messaging only (see the RoomSettings.permissions parameter).

Example

This example sends a public message and handles the respective event.

function someMethod()
{
	sfs.addEventListener(SFS2X.SFSEvent.PUBLIC_MESSAGE, onPublicMessage, this);

	// Send a public message
	sfs.send(new SFS2X.PublicMessageRequest("Hello everyone!"));
}

function onPublicMessage(evtParams)
{
	// As messages are forwarded to the sender too,
	// I have to check if I am the sender

	var sender = evtParams.sender;

	if (sender == sfs.mySelf)
		console.log("I said: " + evtParams.message);
	else
		console.log("User " + sender.name + " said: " + evtParams.message);
}

Parameters

Name Type Optional Description

message

 

 

The message to be sent to all the users in the target Room.

params

 

Yes

A SFSObject containing additional custom parameters to be sent to the message recipients (for example the color of the text, etc).

Defaults to null.

targetRoom

 

Yes

The SFSRoom object corresponding to the Room where the message should be dispatched; if null, the last Room joined by the user is used.

Defaults to null.

See also
SmartFox#send
SFSEvent.PUBLIC_MESSAGE