If your database is supporting analytical functions, you can consolidate into unique pairs before calculating count.
SELECT
LEAST("from", "to") AS user1,
GREATEST("from", "to") AS user2,
COUNT(*) AS count
FROM
messages
GROUP BY
LEAST("from", "to"),
GREATEST("from", "to");